Challenge

Project 1 - Data Handling and Interpolation

Project 1. Simple harmonic oscillator (SHO):

  • When a force that is directed towards the mean position and proportional to the displacement at any given instant from this position causes a particle to oscillate about its mean position along a straight line, the motion of the particle is referred to as a simple harmonic, and the oscillating particle is called a simple harmonic oscillator.
  • Solving the SHO equation [m. dx² / dt² = -k.x] in m-script.
  • Converting a second-order differential equation into a first-order differential equation.

let v= dx/dt. then on differentiating w.r.t time 't'.

we get. dv/dt = d''x/dt''. after that, the system became

dx/dt = v & dv/dt = -kx/m.

  • following expression used in the Matlab m-script function.

prompt = {'Enter mass (m) - ', 'Enter spring constant (k): '};

dlg = 'input values';

dims = [1 35]; % dimension of input field

definput = {' ',' '};

answer = inputdlg(prompt,dlg,dims,definput);

m= str2double(answer{1}); %converts string to numerical values

k= str2double(answer{2});

if isnan(m) || isnan(k) %conditional check to verify the validity of input

error('Invalid input: Type valid numeric input.');

end

f = @(t, Y) [Y(2); -k/m * Y(1)];

tspan = [0, 10]; %time span

x0 = 1; v0 = 0; initial values of position and velocity

Y0 = [x0,v0];

[t_ode45, Y_ode45] = ode45(f, tspan, Y0); % for ode45 solution

[t_ode23, Y_ode23] = ode23(f, tspan, Y0); % for ode23 solution

x_ode45 = Y_ode45(:, 1);

v_ode45 = Y_ode45(:, 2);

x_ode23 = Y_ode23(:, 1);

v_ode23 = Y_ode23(:, 2);

figure;

subplot (2, 1, 1);

plot(t_ode45, x_ode45, 'b-');grid; hold;

plot(t_ode23, x_ode23, 'm--');

xlabel('Time');

ylabel('Position (x)');

legend('ode45', 'ode23');

title('Simple Harmonic Oscillator: Position');

subplot (2, 1, 2);

plot(t_ode45, v_ode45, 'b-');hold on;grid;

plot(t_ode23, v_ode23, 'g--');

xlabel('Time');

ylabel('Velocity (v)');

legend('ode45', 'ode23');

title('Simple Harmonic Oscillator: Velocity');

  • The function f is defined as 'f(t,Y) = [Y(2); -k/m*Y91)]'. Here the state vector Y, Y(1) refers to 'x' i.e., position, and Y(2) refers to 'v' i.e., velocity.
  • The time span specifies the time interval.
  • solvers, which are used to solve the differential equation using ode 45 and ode 23.
  • plotting a graph between ode45 and ode23 in two different x-y planes named position and velocity graph.

Project 3. Here is the matlab expression.

function y = pro3(x)

y = sin(x.^2)+cos(x)-2*sin(x)*cos(x);

end

x = linespace(-3*pi,3*pi,1000);

y = evaluateExpression(x);

figure;

plot(x,y,'b--');

xlabel('x');

ylabel('y');

title('Plot of y = sin(x^2) + cos(x) - 2*sin(x)*cos(x)');

grid on;

[~, locs_min] = findpeaks(-y);

[~, locs_max] = findpeaks(y);

hold on;

plot(local_minima(:,1), local_minima(:,2), 'ro', 'MarkerFaceColor', 'r');

hold off;

[x_max, idx_max] = max(y);

y_max = y(idx_max);

hold on;

plot(x_max, y_max, 'go', 'MarkerFaceColor', 'g');

hold off;

disp('Local Minima:');

disp('global_minima');


Project 3. Contour :

  • The ' contour' command in MATLAB is used to create contour plots of 2D functions. Contour plots are useful for visualizing the behavior of a function of two variables over a grid of points. They represent constant levels of the function with different contour lines.

[X, Y] = meshgrid(linspace(-pi, pi, 100)); % Create a grid of x and y values

Z = sin(X) .* cos(Y); % Compute z values

figure;

contour(X, Y, Z);

xlabel('x');

ylabel('y');

title('Contour Plot of z = sin(x) * cos(y)');

colorbar; % Add color bar to show contour levels

grid on;


  • Explanation:
  • 'meshgrid' - creates a grid of x and y values.
  • colorbar - add colour bar to the plot.
  • grid - it gives visualisation of lines.
Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.