You are on page 1of 11

ASSIGNMENT

Course Code 20MTB211A


Course Name Engineering Mathematics – 4
Programme B. Tech.
Department Aerospace Engineering
Faculty Faculty of Engineering and Technology

Name of the Student A U NACHIKETH KUMAR


Reg. No 20ETAS012001
Semester/Year 4/2
Course Leader/s Dr Gireesh D. S.

1
Name: A U Nachiketh Kumar Course: M4-Matlab

Declaration Sheet
Student Name A U Nachiketh Kumar
Reg. No 20ETAS012001
Programme B. Tech Semester/Year 4/2
Course Code 20MTB211A
Course Title Engineering Mathematics – 4
Course Date To
Dr Gireesh D. S. / Dr Shivashankar C. / Dr Somashekhara G. /
Course Leader
Dr. Sumanth Bharadwaj H. S.

Declaration

The assignment submitted herewith is a result of my own investigations and that I have
conformed to the guidelines against plagiarism as laid out in the Student Handbook. All
sections of the text and results, which have been obtained from other sources, are fully
referenced. I understand that cheating and plagiarism constitute a breach of University
regulations and will be dealt with accordingly.

Signature of the
Date
Student
Submission date
stamp
(by Examination & Assessment
Section)
Signature of the Course Leader and date Signature of the Reviewer and date
Name: A U Nachiketh Kumar Course: M4-Matlab

Part – A
1.1 Consider the IVP:
dy
= −y + x√y , y(2) = 2,2 ≤ x ≤ 3.
dx

1.1.1 Determine the solutions of the given IVP at 𝑥=2.25,2.5,2.75,3 by


a. Euler’s method
b. Modified Euler’s method
c. Runge-Kutta method of order 4
using MATLAB and record the output. Plot the graph of absolute error at each
step 𝑥 for each method and comment.

1.1.2 Determine the solution at 𝑥=3 using modified Euler’s method for step size
ℎ= 0.05,0.1,0.2,0.24,0.5,1.0 with the help of a MATLAB program. Tabulate the
solution at 𝑥=3 and plot a graph of relative error vs ℎ and comment.
Name: A U Nachiketh Kumar Course: M4-Matlab

Determine the solutions of the given IVP at 𝑥=2.25,2.5,2.75,3 by


a. Euler’s method

PROGRAM CODE:

%Eulers Method
function [] = EulersMethod(x0,y0,h,xn)
f = @(x,y) -y + x*sqrt(y);
n = round((xn-x0)/h);
x = x0:h:xn;
y(1) = y0;
for i=1:n
y(i+1) = y(i) + h*f(x(i),y(i));
fprintf('The value of y when x is %f will be
%f\n',x(i+1),y(i+1))
end

u= dsolve('Dy=-y + x*sqrt(y)','y(2) = 2','x');


u= eval(u);
Error= abs(u(2,:) - y);
plot(x, Error, 'm*-.')
title('Absolute Error using Eulers method')
legend('Absolute Error','location','northwest')
xlabel('x values')
ylabel('Absolute Error')
grid on
end

% Input: x0 = 2; y0 = 2; xn = 3; h = 0.25;
EulersMethod(x0,y0,h,xn)

OUTPUT:
>> EulersMethod(x0,y0,h,xn)
The value of y when x is 2.250000 will be 2.207107
The value of y when x is 2.500000 will be 2.490999
The value of y when x is 2.750000 will be 2.854680
The value of y when x is 3.000000 will be 3.302596

Engineering Mathematics - 1 4
Name: A U Nachiketh Kumar Course: M4-Matlab

PLOT:

Engineering Mathematics - 1 5
Name: A U Nachiketh Kumar Course: M4-Matlab

Determine the solutions of the given IVP at 𝑥=2.25,2.5,2.75,3 by


b. Modified Euler’s method

PROGRAM CODE:

%Modified Eulers Method


function [] = ModifiedEulersMethod(x0,y0,h,xn)
f = @(x,y) -y + x*sqrt(y);
n = round((xn-x0)/h);
x = x0:h:xn;
y(1) = y0;
for i = 1:n
y(i+1) = y(i) + h*f(x(i),y(i));
y(i+1) = y(i) + (h/2)*(f(x(i),y(i)) +
f(x(i+1),y(i+1)));
fprintf('The value of y when x is %f will be
%f\n',x(i+1),y(i+1))
end

u = dsolve('Dy = -y + x*sqrt(y)','y(2) = 2','x');


u = eval(u);
Error = abs(u(2,:) - y);
plot(x, Error, 'ro-.')
title('Absolute error using Modified Eulers method')
legend('Absolute Error','location','northwest')
xlabel('x values')
ylabel('Absolute Error')
grid on
end

%Input: x0 = 2; y0 = 2; xn = 3; h = 0.25;
ModifiedEulersMethod(x0,y0,h,xn)

OUTPUT:
>> ModifiedEulersMethod(x0,y0,h,xn)
The value of y when x is 2.250000 will be 2.245499
The value of y when x is 2.500000 will be 2.567156
The value of y when x is 2.750000 will be 2.969195
The value of y when x is 3.000000 will be 3.456568

Engineering Mathematics - 1 6
Name: A U Nachiketh Kumar Course: M4-Matlab

PLOT:

Engineering Mathematics - 1 7
Name: A U Nachiketh Kumar Course: M4-Matlab

Determine the solutions of the given IVP at 𝑥=2.25,2.5,2.75,3 by


c. Range Kutta method

PROGRAM CODE:

%Range Kutta Method


function [] = RangeKutta4(x0,y0,h,xn)
f = @(x,y) -y + x*sqrt(y);
n = round((xn-x0)/h);
x = x0:h:xn;
y(1) = y0;
for i = 1:n
k1 = f(x(i),y(i));
k2 = f(x(i) + h/2, y(i) + k1*h/2);
k3 = f(x(i) + h/2, y(i) + k2*h/2);
k4 = f(x(i) + h, y(i) + k3*h);
y(i+1) = y(i) + (h/6)*(k1 + 2*k2 + 2*k3 + k4);
fprintf('Value of y when x is %f will be
%f\n',x(i+1),y(i+1))
end

u = dsolve('Dy = -y + x*sqrt(y)','y(2) = 2','x');


u = eval(u);
Error = abs(u(2,:) - y);
plot(x, Error, 'g*-.')
title('Absolute error using Range Kutta 4th order method')
legend('Absolute Error','location','northwest')
xlabel('x values')
ylabel('Absolute Error')
grid on
end

%Input: x0 = 2; y0 = 2; xn = 3; h = 0.25;
RangeKutta4(x0,y0,h,xn)

OUTPUT:
>> RangeKutta4(x0,y0,h,xn)
Value of y when x is 2.250000 will be 2.244119
Value of y when x is 2.500000 will be 2.564449
Value of y when x is 2.750000 will be 2.965189
Value of y when x is 3.000000 will be 3.451281

Engineering Mathematics - 1 8
Name: A U Nachiketh Kumar Course: M4-Matlab

PLOT:

Engineering Mathematics - 1 9
Name: A U Nachiketh Kumar Course: M4-Matlab

1.1.2 Determine the solution at 𝑥=3 using modified Euler’s method for step size
ℎ= 0.05,0.1,0.2,0.24,0.5,1.0 with the help of a MATLAB program. Tabulate the
solution at 𝑥=3 and plot a graph of relative error vs ℎ and comment.

%Modified Eulers method for given step size

function [y1] = ModifiedEulersMethod_h(x0,y0,h,xn)


f = @(x,y) -y + x*sqrt(y);
n = round((xn-x0)/h);
x = x0:h:xn;
y(1) = y0;
for i = 1:n
y(i+1) = y(i) + h*f(x(i),y(i));
y(i+1) = y(i) + (h/2)*(f(x(i),y(i)) +
f(x(i+1),y(i+1)));
end
y1 = y(end);

%INPUT
%{
Input: x0 = 2; y0 = 2; xn = 3; h = [0.05 0.1 0.2 0.25 0.5
1];

u = dsolve('Dy = -y + t*sqrt(y)','y(2) = 2','t');


t = 3;
u = eval(u(2,:));
for i = 1:length(h)
y(i) = ModifiedEulersMethod_h(x0,y0,h(i),xn);
RelativeError(i) = abs(u - y(i))/u;
fprintf('Solution at x = 3 by Modified Eulers Method
when step size h = %f is %f\n',h(i),y(i));
end
plot(h,RelativeError,'b*:')
title('Relative error of Modified Eulers Method')
xlabel('Step size')
ylabel('Relative Error')
grid on
%}

Engineering Mathematics - 1 10
Name: A U Nachiketh Kumar Course: M4-Matlab

OUTPUT:
Solution at x = 3 by Modified Eulers Method when step size h = 0.050000 is 3.451502
Solution at x = 3 by Modified Eulers Method when step size h = 0.100000 is 3.452146
Solution at x = 3 by Modified Eulers Method when step size h = 0.200000 is 3.454688
Solution at x = 3 by Modified Eulers Method when step size h = 0.250000 is 3.456568
Solution at x = 3 by Modified Eulers Method when step size h = 0.500000 is 3.471601
Solution at x = 3 by Modified Eulers Method when step size h = 1.000000 is 3.522689

PLOT

Engineering Mathematics - 1 11

You might also like