Challenge

Project 1 - Data Handling and Interpolation

Project 1 :

  • Solve the simple harmonic oscillator equation [m . dx² / dt² = -k.x ] for a given m (mass) and k (spring constant) values using ‘ode45’ and ‘ode23’ solver in MATLAB.
  • Plot x, x_dot (ẋ) & x_dot_dot (ẍ).
  • Use the input dialog box to get ‘m’ & ‘k’ values from the user.
  • Compare ‘ode45’ and ‘ode23’ plots and explain.

Code:

Ode45:

x0= [2 ; 1];

dt= 0.1;

T= 20;

tspan = 0:dt:T;

k= 100;

m= 5;

A_matrix = [0 1; -k/m 0]; % formation of matrix

[t,x] = ode45(@(t,x) A_matrix*x, tspan, x0); % solving x using ode45

plot(t,x(:,1), 'b--', t,x(:,2), 'r');

%legend('x', 'xdot');

xlabel('Time t');

ylabel('Solution x');

xdot_dot= (0)*x(:,2)-(-k/m)*x(:,1);

hold on

plot(t, xdot_dot, 'g--');

legend('x', 'xdot', 'xdot-dot');

ode23

x0= [2 ; 1];

dt= 0.1;

T= 20;

tspan = 0:dt:T;

k= 100;

m= 5;

A_matrix = [0 1; -k/m 0]; % formation of matrix

[t,x] = ode23(@(t,x) A_matrix*x, tspan, x0); % solving x using ode45

plot(t,x(:,1), 'b--', t,x(:,2), 'r');

%legend('x', 'xdot');

xlabel('Time t');

ylabel('Solution x');

xdot_dot= (0)*x(:,2)-(-k/m)*x(:,1);

hold on

plot(t, xdot_dot, 'g--');

legend('x', 'xdot', 'xdot-dot');

Output: -> Ode45

Output -> Ode23

Comparison between Ode23 and Ode45 solvers

Ode23 is a three-stage, third-order, Runge-Kutta method. ode45 is a six-stage, fifth-order, Runge-Kutta method. ode45 does more work per step than ode23, but can take much larger steps. For differential equations with smooth solutions, ode45 is often more accurate than ode23. In fact, it may be so accurate that the interpolant is required to provide the desired resolution.

Ode45 is the anchor of the differential equation suite. The MATLAB documentation recommends ode45 as the first choice. And Simulink blocks set ode45 as the default solver. The natural step size that ode23 chooses is frequently just right for display purposes.

Write a program to perform the following steps

  • load altitude vs temperature data from the tables International Standard Atmosphere (ISA) ((click on the link))
  • use the ‘polyfit’ function to determine a regression model: T = f(h) for the above table.
  • evaluate the model determined using the ‘polyval’ function
  • determine the goodness of fit (goodnessOfFit function)
  • Generate comparative plot for the loaded and evaluated data
  • store polyval evaluated data into a text file.

Code:

load("/MATLAB Drive/Elevation.mat")

load("/MATLAB Drive/Temperature.mat")

p = polyfit(h, h1, 1); % ployfit function

f = polyval(p,h); % ployval function

cost_func = 'NMSE'; % NRMSE(1.6417), NMSE(2.6952), MSE(1.7454e+08)

fit = goodnessOfFit(f,h,cost_func); % goodness of fit evaluation

subplot(211);

plot(h,h1,'o',h,f,'-'); grid; hold;

title('Linear regression model');

legend('data','linear fit')

subplot(212);

plot(h,h1); grid; hold; %comparative plot of elevation and temp

title('Elevation Vs Temperature');

Output:

Project 3 :

Create a function to evaluate the following expression.

y= sin(x²) +cos(x) - 2 sin(x) cos(x)

  • Generate the plot and determine all local minima and global maxima values.
  • Use the ‘gtext’ command to highlight the local minima and global maxima value in the graph.

Code:

function y = fcn(x)

x = -10:0.01:10;

a=sin(x);

b=cos(x);

y = sin(x.^2) + b - (a*b');

plot(x,y);

[Maxima,MaxIdx] = findpeaks(y);

%plot maxima

hold on;

plot(x(MaxIdx),Maxima,'r*');

[Minima,MinIdx] = findpeaks(-y);

%plot minima

plot(x(MinIdx),-Minima,'g*');

hold off;

grid; title('Project-3');

end

output:

Project 4 :

Give a brief note on the “contour” command. Illustrate with an example.

Note :

  • For example, take simple code and execute in MATLAB.
  • Attach the same along with the answer body.

Answer:

Contour plots are used to show 3D surfaces by plotting z- slides on a 2D surface. A contour plot is also called a line plot. In contour, we have 3 variables x, y, z. The x, y variables are used to give the values for z, ( z=f(x, y)). The x and y variables are usually in a grid called meshgrid.

There are various contour plots in MATLAB like contour, contourf, contour3, contourc, countourslice, clabel, and fcontour.

contour(Z) creates a contour plot containing the isolines of matrix Z, where Z contains height values on the x-y plane. MATLAB® automatically selects the contour lines to display. The column and row indices of Z are the x and y coordinates in the plane, respectively.

Example:

Code:

Z = peaks;

Z(:,5) = NaN;

contour(Z)

Example(2);

Code:

x = linspace(-2*pi,2*pi);

y = linspace(0,4*pi);

[X,Y] = meshgrid(x,y);

Z = sin(X)+cos(Y);

contour(X,Y,Z)

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.