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.

Solution :-

Program :- second order differential euation using ode 45 solver

given question =[m . dx² / dt² = -k.x ]

here in this equation x_dot_dot=-k*x/m

m=5;% mass
k=2; % spring constant
x0=[2;1]; %defining initial condition of x and x dot
dt=0.1; % time difference
T=20; %max. time
t_span=[0:dt:T]; % time interval
A_matrix=[0 1; -k/m 0]; %formation of matrix
% solve using ode45
[t,x]=ode45(@(t,x) A_matrix*x,t_span,x0); % ode45 does more work per step than ode23


%solve using ode45
plot(t,x(:,1),'m--',t,x(:,2),'g*');
legend('x','x_dot');
xlabel('time');
ylabel('solution x ');

%solving for x_dot_dot (acceleration)
x_dot_dot=(-k/m)*x(:,2);
hold on
plot(t,x_dot_dot,'b--');
legend('x','x_dot','x_dot_dot'); % plotting value

for acceleration (x_dot_dot)

Program :- second order differential euation using ode23 solver

m=5;% mass
k=2; % spring constant
x0=[2;1]; %defining initial condition of x and x dot
dt=0.1; % time difference
T=20; %max. time
t_span=[0:dt:T]; % time interval
A_matrix=[0 1; -k/m 0]; %formation of matrix
% solve using ode23
[t,x]=ode23(@(t,x) A_matrix*x,t_span,x0);
%solve using ode23
plot(t,x(:,1),'m--',t,x(:,2),'g*');
legend('x','x_dot');
xlabel('time');
ylabel('solution x ');

%solving for x_dot_dot (acceleration)
x_dot_dot=(-k/m)*x(:,2);
hold on
plot(t,x_dot_dot,'b--');
legend('x','x_dot','x_dot_dot'); % plotting value

conclusion :-

ode45 does more work per step than ode23.

For differential equations with smooth solutions, ode45 is often more accurate than ode23.

Project 2 :

Write a program to perform the following steps

  • load altitude vs temperature data from the tables International Standard Atmosphere (ISA)
  • 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.

Solution :-

a)

Program :-

clc
clear all;
close all;
warning off;
K=xlsread('mini_project_2.xlsx','sheet1','A3:B52'); % data reading from excel file
E=K(:,1); % extracting 1st column
T=K(:,2); % extracting 2nd column
plot(T,E,'O')
title('Elevation vs Temperature');xlabel('elevation');ylabel('temperature');
a=polyfit(T,E,1) % polyfit function is used for n=1(1st order )

%therefore the polynomial output is y=-0.0029x+270.6452

  • evaluate the model determined using the ‘polyval’ function

program :-

clc
clear all;
close all;
warning off;
K=xlsread('mini_project_2.xlsx','sheet1','A3:B52'); % data reading from excel file
E=K(:,1); % extracting 1st column
T=K(:,2); % extracting 2nd column
plot(T,E,'O');legend('polyfit analysis');
title('Elevation vs Temperature');xlabel('elevation');ylabel('temperature');
a=polyfit(T,E,1) ; % polyfit function is used

%therefore the polynomial output is z=-0.0029T+270.6452

z=polyval(a,T);
hold on ;
plot(T,z,'*');legend('polyval analysis');
title('regression analysis ');

Results:- regression analysis done.

  • determine the goodness of fit (goodnessOfFit function)

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.

program:-

clc
clear all
close all
warning off
funh=@(x) sin(x)^2+cos(x)-2*sin(x)*cos(x); ............function defined
fplot(funh); ........function plot
grid on;
mi=fminsearch(funh,2); ...........finding minima
hold on ;
plot(mi,funh(mi),'ro','linewidth',5);
gtext('minima');
ginput(1);

finding global maxima

clear all;
close all;
warning off;
syms x;
y=sin(x)^2+cos(x)-2*sin(x)*cos(x);
ezplot(y,[0,10]);
g=diff(y);
s=solve(g);
subs(y,2), subs(y,1)
gtext('maxima');
ginput(1) ,

Project 4 :

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

A contour plot is a graphical method to visualize the 3-D surface by plotting constant Z slices called contours in a 2-D format. The contour plot is an alternative to a 3-D surface plot

The contour plot is formed by:

  • Vertical axis: Independent variable 2
  • Horizontal axis: Independent variable 1
  • Lines: iso-response values, can be calculated with the help (x,y).

The independent variable usually restricted to a regular grid. The actual techniques for determining the correct iso-response values are rather complex and almost always computer-generated.

The contour plot is used to depict the change in Z values as compared to X and Y values. If the data (or function) do not form a regular grid, you typically need to perform a 2-D interpolation to form a regular grid.

For one variable data, a run sequence/ histogram is considered necessary. For two-variable data, a scatter plot is considered necessary. The contour plots can also polar co-ordinates (r,theta) instead of traditional rectangular (x, y, z) coordinates.

Types of Contour Plot:

  • Rectangular Contour plot: A projection of 2D-plot in 2D-rectangular canvas. It is the most common form of the contour plot.
  • Polar contour plot: Polar contour plot is plotted by using the polar coordinates r and theta. The response variable here is the collection of values generated while passing r and theta into the given function, where r is the distance from origin and theta is the angle from the positive x axis.
  • Ternary contour plot: Ternary contour plot is used to represent the relationship between 3 explanatory variables and the response variable in the form of a filled triangle.

Contour plot can be plotted in different programming languages:

  • Python/ Matplotlib: Contour plot can be plotted using plt.contour or plt.contourf functions, where plt is matplotlib.pyplot. The difference between these two that plot.contour generates hollow contour plot, the plt.contourf generated filled.
  • Matlab: functions such as contourf (2d-plot) and contour3 (3D-contour) can be used for contour plotting
  • R: can create a contour plot with filled.contour functions in R.

x = -2:0.2:2;
y = -2:0.2:3;
[X,Y] = meshgrid(x,y);
Z = X.*exp(-X.^2-Y.^2);
contour(X,Y,Z,'ShowText','on')

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.