Lab Manual - New-NSM
Lab Manual - New-NSM
“Techno-Social Excellence”
Marathwada Mitra Mandal’s
Institute of Technology, Lohgaon, Pune - 411 047
Lab Manual
Prepared by,
Prof. Sudhir S. More
Page 1
Artificial Intelligence and Machine Learning TE-Mechanical-2019
Page 2
Artificial Intelligence and Machine Learning TE-Mechanical-2019
Page 3
Artificial Intelligence and Machine Learning TE-Mechanical-2019
Statement:
Statement:
Page 4
Artificial Intelligence and Machine Learning TE-Mechanical-2019
Course Outcomes:
CO 1 302041.pr.1 Solve the Roots of Equations by using numerical method in MATLAB software
CO 2 302041.pr.2 Solve the Differential equation by using numerical method in MATLAB software
Use in MATLAB software and numerical method to fit the suitable curve for given
CO 4 302041.pr.4 set of data
CO 6 302041.pr.6 Determine statistical measures of given set of data using software’s like Minitab
Analyse the mechanical engineering application dataset and find the solution by
CO 7 302041.pr.7 appropriate numerical method by using programming knowledge for
CO PO Mapping:
CO. No. PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12 PSO1 PSO2
302041.pr.1 PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12 PSO1 PSO2
302041.pr.2 3 2 2 2
302041.pr.3 3 2 2 2
302041.pr.4 3 2 2 2
302041.pr.5 3 2 2 2
302041.pr.6 3 2 2 2
302041.pr.7 3 2 2 2 2
Average
Page 5
Artificial Intelligence and Machine Learning TE-Mechanical-2019
INDEX
Exp. Page
Title of Experiment
No. No
Page 6
Artificial Intelligence and Machine Learning TE-Mechanical-2019
EXPERIMENT NO. 1
Programs
% Matlab program for Newton-Raphson method
% when accuracy is given
clc;
clear;
f = inline('exp(x)*cos(x)-1.2');
df = inline('exp(x)*(cos(x)-sin(x))');
ddf = inline('-2*exp(x)*sin(x)');
x1=input('\n Enter the value of initial guess x1: ');
acc=input('\n Enter the given value of accuracy: ');
f(x1);
df(x1);
ddf(x1);
while(abs(f(x1)*ddf(x1))<0)
fprintf('\n Enter value of x1 again: ');
f(x1);
d(x1);
ddf(x1);
end
x2=x1-(f(x1)/df(x1));
while(abs(x1-x2)>acc)
x1=x2;
f(x1);
df(x1);
x2=x1-(f(x1)/df(x1));
end
fprintf('\n The root of given equation is =%f',x2);
Page 7
Artificial Intelligence and Machine Learning TE-Mechanical-2019
Page 8
Artificial Intelligence and Machine Learning TE-Mechanical-2019
Solver
% X = FZERO(FUN,X0) tries to find a zero of the function FUN near X0
% X = FZERO(FUN,X0), where X0 is a scalar value, uses X0 as a starting guess.
% FZERO looks for an interval containing a sign change for FUN and containing X0..
Example No.1
X=fzero(inline('exp(x)*cos(x)-1.4'),0);
X
Example No.2
To use the default tolerance and to display iteration information.
X = fzero(inline('exp(x)*cos(x)-1.4'),0,optimset('disp','iter'));
X
Page 9
Artificial Intelligence and Machine Learning TE-Mechanical-2019
EXPERIMENT NO. 2
Program
% Matlab Program for Euler's Method
clc;
f=inline('((x-y^2))');
x0=input('\n Enter the given value of x0=');
y0=input('\n Enter the given value of y0=');
h=input('\n Enter the given value of step size h=');
xn=input('\n Enter the given value of xn at which yn is to be calculated=');
n=(xn-x0)/h;
fprintf('\n The total no. of steps are=%d',n);
% Assumption
x(1)=x0;
y(1)=y0;
% Loop to calculate Approximate solution using Euler formula
fprintf('\n\t X \t\t\ty ');
for i=1:1:n
x(i+1)=x0+i*h;
y(i+1)=y(i)+h*f(x(i),y(i));
fprintf('\n%f \t%f \t%f',x(i+1),y(i+1));
end
Output
Page 10
Artificial Intelligence and Machine Learning TE-Mechanical-2019
X y
1.000000 0.000000
2.000000 1.000000
3.000000 2.000000
Page 11
Artificial Intelligence and Machine Learning TE-Mechanical-2019
yc=y(i)+((h/2)*(f(x(i),y(i))+f(x(i+1),yp)));
end
y(i+1)=yc;
fprintf('\n%f \t%f \t%f',x(i+1),y(i+1));
end
Output
Enter accuracy=0.001
Page 12
Artificial Intelligence and Machine Learning TE-Mechanical-2019
Output
Page 13
Artificial Intelligence and Machine Learning TE-Mechanical-2019
Output
Page 14
Artificial Intelligence and Machine Learning TE-Mechanical-2019
Solver
Matlab Syntax to solve first order ODE
% ODE45 Solve non-stiff differential equations, medium order method.
% [Xe, Ye] = ODE45 (ODEFUN, XSPAN, Y0) with XSPAN = [X0: h: XFINAL]
% above syntax integrates the system of differential equations y' = f (x, y) from time X0 to %
XFINAL with initial condition Y0 at each step size h.
Example
clc;
f=inline('(y-x)/(y+x)');
x0=0;
xn=0.1;
y0=1;
h=0.01;
xspan=[x0:h:xn];
[xe,ye]=ode45(f,xspan,y0);
[xe,ye]
plot(xe,ye(:,1),'b*-');
Firstly, Create separate F.m file to define given second order ODE as follows in new file
%function dy=F(x,y);
%dy=[y(2);(2*x*y(2))-(4*y(1))+4];
Where
% dy/dx=y(1)=phi=inline('z')
Page 15
Artificial Intelligence and Machine Learning TE-Mechanical-2019
% dz/dx=y(2)=f=inline('2xz-4y+4')
% Then, solve diff. equation using ode45 as follows
clc;
type F
x0=0;
y0=1;
z0=-2;
xn=0.4;
h=0.1;
xspan=[x0:h:xn];
[xe,ye]=ode45('F',xspan,[y0;z0]);
[xe,ye]
plot(ye);
xlabel('x');
ylabel('solution y');
title('Solution of second order ODE')
legend('Blue:y(1)=dy/dx','Green:y(2)=dz/dx');
Example2
Firstly, Create separate F.m file to define given second order ODE as follows in new file
function dy=F(x,y)
dy=[y(2);x*(y(2)^2)-y(1)^2];
%dy/dx=y(1)=phi=inline('z');
%dz/dx=y(2)=f=inline('(x(z^2))-y^2');
Then, solve diff. equation using ode45 as follows in command window
type F
x0=0;
y0=1;
z0=0;
xn=0.2;
h=0.2;
xspan=[x0:h:xn];
Page 16
Artificial Intelligence and Machine Learning TE-Mechanical-2019
[xe,ye]=ode45('F',xspan,[y0;z0]);
[xe,ye]
plot(ye);
xlabel('x');
ylabel('solution y');
title('Solution of second order ODE')
legend('Blue:y(1)=dy/dx','Green:y(2)=dz/dx');
Page 17
Artificial Intelligence and Machine Learning TE-Mechanical-2019
EXPERIMENT NO. 3
Function y= f(x)
Y=x^3+2x-1
Page 18
Artificial Intelligence and Machine Learning TE-Mechanical-2019
Function y= f(x)
Y=x^3+2x-1
X0=input (‘enter the initial limit x0 :’);
Xn=input (‘enter the initial limit xn:’);
n= input (‘enter the valueof n :’);
While (mod (n, 2)=0)
n=(‘input the value of n again:’)
end
h=(xn-x0/2);
ans=0;
for i=1:n-1
if (mod(I,2)=0)
ans=ans=4*(f(x0+i*));
else
ans=ans+2*(f(x0+i*h));
end
end
ans1=f(x0)+f(xn);
area=ans+ans1
area=area*(h/3);
fprintf(‘/n total area =%f,area);
Page 19
Artificial Intelligence and Machine Learning TE-Mechanical-2019
OR
F = inline('1./(x.^3-2*x-5)');
Q = quadl(F,0,2);
Double Integeration
% DBLQUAD Numerically evaluates double integral.
% DBLQUAD (F, Xmin, Xmax, Ymin, Ymax) evaluates the double integral F(X, Y)
% F(X, Y) should accept a vector X and a scalar Y and return a vector of values of integrand.
Example No.1
Q = dblquad(inline('y*sin(x)+x*cos(y)'), pi, 2*pi, 0, pi);
Q
Example No.2
Q=dblquad(inline('sqrt(max(1-(x.^2+y.^2),0))'),-1,1,-1,1);
Q
Example No.3
Q=dblquad(inline('exp(x+y)'),0,1,0,1);
Page 20
Artificial Intelligence and Machine Learning TE-Mechanical-2019
EXPERIMENT NO. 4
Title: Program of Curve fitting using least square technique using suitable programing
Linear equation
% Matlab Program to fit straight line/First degree curve (y=ax+b)
clc;
clear;
n=input('\nEnter the value of data entries n= ');
for i=1:1:n
x(i)=input('Enter the values of x= ');
y(i)=input('Enter the values of y= ');
end
sx=0;
sy=0;
sxx=0;
sxy=0;
for i=1:1:n
sx=sx+x(i);
sy=sy+y(i);
sxx=sxx+x(i)*x(i);
sxy=sxy+x(i)*y(i);
end
delta=sxx*n-sx*sx;
delta1=sxy*n-sy*sx;
delta2=sxx*sy-sx*sxy;
a=delta1/delta;
b=delta2/delta;
fprintf('\nThe values of constants a=%f and b=%3f',a,b);
fprintf('\nThe equation of straight line is y=%2.5f x + %2.5f',a,b);
% Plotting best fit curve y=ax+b using points (x, ya) and observed curve using (x, y)
for i=1:1:n
Page 21
Artificial Intelligence and Machine Learning TE-Mechanical-2019
ya(i)=a*x(i)+b; %To find the calculated ya using eq. ya=ax+b as per solution
end
hold on
plot(x,y,'b*-');
plot(x,ya,'r+-');
xlabel('X');
ylabel('Y');
title('Graph of first degree curve using least square criteria');
b1=delta2/delta;
a=10^a1;
b=10^b1;
fprintf('\nThe values of constants a1=%f and b1=%f',a1,b1);
fprintf('\nThe values of constants a=%f and b=%f',a,b);
fprintf('\nThe power equation is y=%2.4f * %2.4f ^x',a,b);
% Plotting best fit curve y=ab^x using points (x, ya) and observed curve using (x, y)
for i=1:1:n
ya(i)=a*power(b,x(i)); %To find the calculated ya using equation ya=ab^x
end
hold on
plot(x,y,'b*-')
plot(x,ya,'r*--')
xlabel('X');
ylabel('Y');
title('Graph of power function y=ab^x curve using least square criteria');
Page 23
Artificial Intelligence and Machine Learning TE-Mechanical-2019
sx=sx+X(i);
sy=sy+Y(i);
sxx=sxx+X(i)*X(i);
sxy=sxy+X(i)*Y(i);
end
delta=sxx*n-sx*sx;
delta1=sxy*n-sy*sx;
delta2=sxx*sy-sx*sxy;
a1=delta1/delta;
b1=delta2/delta;
a=exp(b1);
b=a1;
fprintf('\n The values of constants a1=%f and b1=%f',a1,b1);
fprintf('\n The values of constants a=%f and b=%f',a,b);
fprintf('\n The equation of exponential curve is y=%2.4fe^%2.4fx',a,b);
% Plotting best fit curve y=ae^bx using points (x, ya) and observed curve using (x, y)
for i=1:1:n
ya(i)=a*exp(b*x(i)); %To find the calculated ya using equation ya=aebx
end
hold on
plot(x,y,'b*-')
plot(x,ya,'r*--')
xlabel('X');
ylabel('Y');
title('Graph of exponential curve using least square criteria');
Page 24
Artificial Intelligence and Machine Learning TE-Mechanical-2019
Curve fitting
Page 26
Artificial Intelligence and Machine Learning TE-Mechanical-2019
EXPERIMENT NO. 5
Quadratic Equation
% Matlab Program to fit second degree curve/parabola/Quadratic Equation (y=ax^2+bx+c)
clc;
clear;
n=input('\nEnter the value no. of data entries n= ');
for i=1:1:n
x(i)=input('Enter the values of x= ');
y(i)=input('Enter the values of y= ');
end
sx=0;
sy=0;
sxx=0;
sxxx=0;
sxxxx=0;
sxy=0;
sxxy=0;
for i=1:1:n
sx=sx+x(i);
sy=sy+y(i);
sxx=sxx+x(i)*x(i);
sxxx=sxxx+x(i)*x(i)*x(i);
sxxxx=sxxxx+x(i)*x(i)*x(i)*x(i);
sxy=sxy+x(i)*y(i);
sxxy=sxxy+x(i)*x(i)*y(i);
end
delta=sxxxx*(sxx*n-sx*sx)-sxxx*(sxxx*n-sxx*sx)+sxx*(sxxx*sx-sxx*sxx);
delta1=sxxy*(sxx*n-sx*sx)-sxxx*(sxy*n-sy*sx)+sxx*(sxy*sx-sy*sxx);
delta2=sxxxx*(sxy*n-sy*sx)-sxxy*(sxxx*n-sxx*sx)+sxx*(sxxx*sy-sxx*sxy);
Page 27
Artificial Intelligence and Machine Learning TE-Mechanical-2019
delta3=sxxxx*(sxx*sy-sx*sxy)-sxxx*(sxxx*sy-sxx*sxy)+sxxy*(sxxx*sx-sxx*sxx);
a=delta1/delta;
b=delta2/delta;
c=delta3/delta;
fprintf('\nThe values of constants a=%f ,b=%f and c=%f',a,b,c);
fprintf('\nThe equation of second degree curve is y=%2.3f x^2 + %2.3f x + %2.3f',a,b,c);
% Plotting best fit curve y=ax^2+bx+c using points (x, ya) and observed curve using (x, y)
for i=1:1:n
ya(i)=a*x(i)*x(i)+b*x(i)+c; %To find the calculated ya using equation ya=ax^2+bx+c
end
hold on
plot(x,y,'b*-');
plot(x,ya,'r+-');
xlabel('X');
ylabel('Y');
title('Graph of second degree curve using least square criteria');
Page 28
Artificial Intelligence and Machine Learning TE-Mechanical-2019
EXPERIMENT NO. 6
Descriptive statistics summarize and describe the prominent features of data. Use Display
Descriptive Statistics to determine how many book orders were delivered on time, how many
were late, and how many were initially back ordered for each shipping center.
Display descriptive statistics
1. Open the data set, Shipping Data.MTW.
2. Choose Stat > Basic Statistics > Display Descriptive Statistics.
3. In Variables, enter Shipping Cost.
4. In By variables (optional), enter Center Status
Page 29
Artificial Intelligence and Machine Learning TE-Mechanical-2019
5. Click Statistics.
6. Uncheck First quartile, Median, Third quartile, N nonmissing, and N missing
7. Check N total.
Page 30
Artificial Intelligence and Machine Learning TE-Mechanical-2019
Page 31
Artificial Intelligence and Machine Learning TE-Mechanical-2019
Page 32
Artificial Intelligence and Machine Learning TE-Mechanical-2019
EXPERIMENT NO. 7
Title: One program based mini project using mechanical engineering application dataset
Objective: - Students should develop a online calculator for a mechanical system using
programmable language
Page 33