Mth643 Solved Questions Lec01-08
Mth643 Solved Questions Lec01-08
L02_Q04: Find the value of the following polynomials at x=2 using subs command.
𝑝 𝑥, 𝑦 = 𝑥 2 + 3𝑦 2 𝑥 + 10,𝑢 𝑥 = 𝑥 3 + 4𝑥 + 10 (Follow P#036-42 - LECT#02)
clear all
close all
clc
syms x y p u
p=x^2+3*y^2*x+10
subs(p,x,2)
u=x^3+4*x+10
subs(u,x,2)
MTH643 - INTRODUCTION TO MATLAB
From: ABU SULTAN
Page 2 of 22
Important and Solved Questions for Mid Term Exam
L03_Q05: Find the solution of 𝑥 2 − 𝑦 = 2 𝑎𝑛𝑑 𝑦 − 2𝑥 = 5 (P#066-68 - LECT#03)
close all
clear all
clc
syms x y
[x,y]=solve('x^2-y=2','y-2*x=5')
x(1)
y(1)
L04_Q08: Plot the graph of 𝑒 −𝑥 𝑎𝑛𝑑 𝑆𝑖𝑛(𝑥) on the interval [0, 8] using ezplot, hold on and
hold off commands. Label the horizontal axis with x and vertical axis with y. Finally gives
the title “Multiple Graphs”. (P#90-92 - LECT#04)
%Plot the graph of e^(-x) and Sin(x)
%on the interval [0, 8] using ezplot, hold on and hold off commands.
%Label the horizontal axis with x and vertical axis with y.
%Finally gives the title “Multiple Graphs"
clc
clear all
close all
syms x
ezplot(exp(-x),[0,8])
hold on
ezplot(sin(x),[0,8])
hold off
xlabel('x')
ylabel('y')
title('Multiple Graphs')
grid
MTH643 - INTRODUCTION TO MATLAB
From: ABU SULTAN
Page 3 of 22
Important and Solved Questions for Mid Term Exam
L04_Q09: Plot the graph of 𝑒 −𝑥 𝑎𝑛𝑑 𝑆𝑖𝑛(𝑥) on the interval [0, 8] using plot, hold on and
hold off commands. Label the horizontal axis with x and vertical axis with y. Finally gives
the title “Multiple Graphs”. (P#90-92 - LECT#04)
%Plot the graph of e^(-x) and Sin(x)on the interval [0, 8] using plot, hold on and hold off.
%Label the horizontal axis with x and vertical axis with y.
%Finally gives the title “Multiple Graphs"
clc
clear all
close all
X=0:0.1:8;
plot(X,exp(-X))
hold on
plot(X,sin(X))
hold off
xlabel('x')
ylabel('y')
title('Multiple Graphs')
grid
L04_Q10: Plot the parametric graph of the circle with center at (0; 0), radius 1, and with
increment of 0:01 in the domain set. Label the horizontal axis with x and vertical axis with y.
Make the shape of the graph square. Keeps the title of the graph "Parametric plot of the
circle". (P#98-100 - LECT#04)
%Plot the parametric graph of the circle with center at (0; 0),
% with radius 1 and with increment of 0:01 in the domain set.
clc
clear all
close all
t=0:0.01:1;
plot(cos(2*pi*t),sin(2*pi*t))
xlabel('x')
ylabel('y')
title('Parametric Plot of the Circle')
grid
L04_Q11: Plot the contour plots of the expression x2 + y2 using meshgrid and contour
commands. Label the horizontal axis with x and vertical axis with y. Make the shape of the
graph square. Keeps the title of the graph "Contour plotting of squares".
(P#102-104 - LECT#04)
%Plot the contour plots of the expression x2 + y2 using meshgrid and contour commands.
%Label the horizontal axis with x and vertical axis with y.
%Make the shape of the graph square.
%Keeps the title of the graph "Contour plotting of squares".
clc
clear all
close all
[X,Y]=meshgrid(-2:0.1:2,-2:0.1:2);
contour(X,Y,X.^2+Y.^2)
axis square
xlabel('x')
ylabel('y')
title('Contour Plotting of Squares')
MTH643 - INTRODUCTION TO MATLAB
From: ABU SULTAN
Page 4 of 22
Important and Solved Questions for Mid Term Exam
L04_Q12: Plot the graph of 𝑥 3 + 𝑥 2 + 4 on the interval -2, 2] using ezplot, hold on and hold
off commands. Label the horizontal axis with x and vertical axis with y. Finally gives the title
“𝑥 3 + 𝑥 2 + 4”. (Follow P#90-92 - LECT#04)
%Plot the graph of x^3+x^2+4 on the interval -2, 2] using ezplot, hold on and hold off
commands.
%Label the horizontal axis with x and vertical axis with y.
%Finally gives the title “x^3+x^2+4”.
clc
clear all
close all
syms x
x=x^3+x^2+4
ezplot(x,[-2,2])
axis square
xlabel('x')
ylabel('y')
title('x^3+x^2+4')
L04_Q13: Plot the graph of the function sin(x) + cos(x) on the interval [0; 1], using plot
command. Label the horizontal axis with x and vertical axis with y. Make the shape of the
graph square. Keeps the title of the graph "The Graph of sin(x) + cos(x)". Keeps the layout of
the graph as grid. (Follow P#98-100 - LECT#04)
%Plot the graph of the function sin(x) + cos(x) on the interval [0; 1], using plot command.
%Label the horizontal axis with x and vertical axis with y.
%Make the shape of the graph square.
%Keeps the title of the graph "The Graph of sin(x) + cos(x)".
%Keeps the layout of the graph as grid.
clc
clear all
close all
X=0:0.01:1;
plot(X,(sin(X)+cos(X)))
xlabel('x')
ylabel('y')
axis square;
title('The Graph of sin(x) + cos(x)');
grid
L04_Q14: Plot the multiple curves of the functions sin(x) + cos(x) and e-x + x2 on the interval
[0; 4], using ezplot command. Label the horizontal axis with x and vertical axis with y. Make
the shape of the graph square. Keeps the title of the graph "The Graph of Multiple curves
using ezplot command ". Keeps the layout of the graph as grid.
(Follow P#90-92 - LECT#04)
%Plot the multiple curves of the functions sin(x) + cos(x) and e-x + x2
%on the interval [0; 4], using ezplot command.
%Label the horizontal axis with x and vertical axis with y.
%Make the shape of the graph square.
%Keeps the title of the graph "The Graph of Multiple curves using ezplot command ".
%Keeps the layout of the graph as grid.
clc
clear all
close all
MTH643 - INTRODUCTION TO MATLAB
From: ABU SULTAN
Page 5 of 22
Important and Solved Questions for Mid Term Exam
syms x
ezplot('sin(x)+cos(x)',[0,4])
hold on
ezplot('exp(-x)+x^2',[0,4])
xlabel('x')
ylabel('y')
axis square
title('The Graph of Multiple curves using ezplot command ')
grid
L04_Q15: Plot the contour plots of the circles x2+y2 of radius 1,2, 2, 3 .Label the
horizontal axis with x and vertical axis with y. Make the shapes of the graph square. Keeps
the title of the graph “Contour Plots of Circles”. Keeps the layout of the graph grid.
(P#109 - EXERCISE – LECT#04)
%Plot the contour plots of the circles x2+y2 of radius 1,2,?2,?3
%Label the horizontal axis with x and vertical axis with y.
%Make the shapes of the graph square.
%Keeps the title of the graph “Contour Plots of Circles”.
%Keeps the layout of the graph grid.
clc
clear all
close all
syms a b
[X,Y]=meshgrid(-2:0.1:2,-2:0.1:2);
contour(X,Y,X.^2+Y.^2-1);
contour(X,Y,X.^2+Y.^2-2);
contour(X,Y,X.^2+Y.^2-sqrt(2));
contour(X,Y,X.^2+Y.^2-sqrt(3));
axis square
xlabel('x-axis')
ylabel('y-axis')
title('Contour Plots of Circles')
grid
L04_Q16: Plot the graph of sin(x)+cos(x) and e(-x)+x^2 on the interval [0, 4] using plot,
hold on and without use of hold on and hold off commands. Label the horizontal axis with x
and vertical axis with y. Finally gives the title “The Graph of Multiple Curves using plot
command".(P#108 - EXERCISE – LECT#04)
%Plot the graph of sin(x)+cos(x) and exp(-x)+x^2
%on the interval [0, 4] using plot command and without use of hold on and hold off
commands.
%Label the horizontal axis with x and vertical axis with y.
%Finally gives the title “The Graph of Multiple Curves using plot command".
%Keeps the layout of the graph as grid.
clc
clear all
close all
x=0:0.1:4;
f1=sin(x)+cos(x);
f2=exp(-x)+x.^2
plot(x,f1,x,f2)
xlabel('x')
MTH643 - INTRODUCTION TO MATLAB
From: ABU SULTAN
Page 6 of 22
Important and Solved Questions for Mid Term Exam
ylabel('y')
title('The Graph of Multiple Curves using plot command')
grid
L05_Q17: Find the first-order, second-order, and fourth-order derivatives of the function
𝑓 𝑥 = 𝑥10 + 2 ∗ 𝑥 2 + 𝑆𝑖𝑛 𝑥 Using diff command. Plot the multiple graphs of the
outputs using ezplot command on the interval [-𝜋, 𝜋]. Label the horizontal axis with x and
vertical axis with y. Keeps the title "Multiple plot of the function and its derivatives". All the
coding should be written using Script M–File.
(P#144 - EXERCISE – LECT#05)+ (P#174 - EXERCISE – LECT#06)
%Find the first-order, second-order, and fourth-order derivatives of the
functionf(x)=x^10+2*(x^2 )+Sin (x) Using diff command.
%Plot the multiple graphs of the outputs using ezplot command on the interval [-?,?].
%Label the horizontal axis with x and vertical axis with y.
%Keeps the title "Multiple plot of the function and its derivatives".
%All the coding should be written using Script M–File
clc
clear all
close all
syms x
f=x^10+2*x^2+sin(x);
f1=diff(f,1)
f2=diff(f,2)
f4=diff(f,4)
ezplot(f,[-pi,pi])
hold on
ezplot(f1,[-pi,pi])
hold on
ezplot(f4,[-pi,pi])
hold off
xlabel('x-axis')
ylabel('y-axis')
title('Multiple plot of the function and its derivatives')
grid
L05_Q18: Find the first-order, fourth-order, and fifth-order derivatives of the function
𝑓 𝑥 = 𝐼𝑛 𝑥 + 𝑆𝑖𝑛(𝑥) using diff command. Plot the multiple graphs of the outputs using plot
command on the interval [0, 1] and without using hold on and hold off commands Label the
horizontal axis with x and vertical axis with y. Keeps the title "Multiple plot of the function
and its derivatives". All the coding should be written using Script M–File.
(P#145 - EXERCISE – LECT#05) + (P#175 - EXERCISE – LECT#06)
%Find the first-order, fourth-order, and fifth-order derivatives of the function
%f(x)=In x+Sin(x)using diff command.
%Plot the multiple graphs of the outputs using plot command on the interval [0, 1]
%and without using hold on and hold off commands.
%Label the horizontal axis with x and vertical axis with y.
%Keeps the title "Multiple plot of the function and its derivatives".
%All the coding should be written using Script M–File
clc
close all
clear all
syms x
MTH643 - INTRODUCTION TO MATLAB
From: ABU SULTAN
Page 7 of 22
Important and Solved Questions for Mid Term Exam
f=log(x)+sin(x)
f1=diff(f,1)
f4=diff(f,4)
f5=diff(f,5)
x=0:0.1:1;
f11=eval(vectorize(f1));
f44=eval(vectorize(f4));
f55=eval(vectorize(f5));
plot(x,f11,x,f44,x,f55)
xlabel('x-axis')
ylabel('y-axis')
title('Multiple plot of the function and its derivatives')
grid
L05_Q19: Find the first-order, fourth-order, and fifth-order derivatives of the function
𝑓 𝑥 = 𝐼𝑛 𝑥 ∗ 𝑦 + 𝑆𝑖𝑛(𝑥 2 ) using diff command. Plot the multiple graphs of the outputs using
plot command on the interval [0, 1] and without using hold on and hold off commands Label
the horizontal axis with x and vertical axis with y. Keeps the title "Multiple plot of the
function and its derivatives". All the coding should be written using Script M–File.
(P#145 - EXERCISE – LECT#05) + (P#176 - EXERCISE – LECT#06)
%Find the first-order, fourth-order, and fifth-order derivatives of the function
%f(x)=In x*y+Sin(x^2) using diff command.
%Plot the multiple graphs of the outputs using plot command on the interval [0, 1]
%and without using hold on and hold off commands.
%Label the horizontal axis with x and vertical axis with y.
%Keeps the title "Multiple plot of the function and its derivatives".
%All the coding should be written using Script M–File
clc
clear all
close all
syms x y
f=log(x*y)+sin(x^2)
d1=diff(f,1)
d4=diff(f,4)
d5=diff(f,5)
x=0:0.1:1;
d11=eval(vectorize(d1));
d44=eval(vectorize(d4));
d55=eval(vectorize(d5));
plot(x,d11,x,d44,x,d55)
xlabel('x-axis')
ylabel('y-axis')
grid
title('Multiple plot of the function and its derivatives')
L05_Q20: Find the derivative of the following function with respect to x, and t variables
using diff command. Moreover plot the multiple graph of the function and their derivatives
by defining X and Y using meshgrid command, Then define Z and create a surface plot:
2 2
f x, t = xe−x −t x, t ∈ −2,2 .
(P#122 – LECT#05)
%Find the derivative of the following function with respect to x, and t variables using diff
command.
MTH643 - INTRODUCTION TO MATLAB
From: ABU SULTAN
Page 8 of 22
Important and Solved Questions for Mid Term Exam
%Moreover plot the multiple graph of the function and their derivatives by defining X and Y
using meshgrid command.
%Then define Z and create a surface plot:
%function is f(x,t)=xe^(-x^2-t^2 ) x,t?[-2,2]
clc
clear all
close all
syms x t
f=x*exp(-x^2-t^2);
f1=diff(f,x)
f2=diff(f,t)
[x,t]=meshgrid(-2:0.2:2,-2:0.2:2);
ff=eval(vectorize(f));
f11=eval(vectorize(f1));
f22=eval(vectorize(f2));
surf(x,t,ff)
hold on
surf(x,t,f11)
hold on
surf(x,t,f22)
hold off
L05_Q21: Find the first-order, second-order, and fourth-order derivatives of the function
f x = 2xy + (xy)3 with respect to variable y using diff command.
%Find the first-order, second-order, and fourth-order derivatives of the function
%f(x)=2xy+(xy)^3 with respect to variable y using diff command.
syms x y
f=2*x*y+(x*y)^3
diff(f,y,1)
diff(f,y,2)
diff(f,y,4)
L05_Q22: Find the integral of the function f x = x 2 + 10x + 4 using int command. Plot the
multiple graphs of the function and its output using plot command. Label the horizontal axis
with x and vertical axis with y. Keeps the title "Graph of function and its integral"
%Find the integral of the function f(x)=x^2+10x+4 using int command.
%Plot the multiple graphs of the function and its output using plot command.
%Label the horizontal axis with x and vertical axis with y.
%Keeps the title "Graph of function and its integral"
clc
clear all
close all
syms x
f=int(x^2+10*x+4)
x=-2:0.01:2;
plot(x,x.^2+10.*x+4,(x.*(x.^2 + 15.*x + 12))./3,x)
xlabel('x-axis')
ylabel('y-axis')
title('Graph of function and its integral')
grid
MTH643 - INTRODUCTION TO MATLAB
From: ABU SULTAN
Page 9 of 22
Important and Solved Questions for Mid Term Exam
L05_Q23: Differentiate the following functions using diff command. Moreover plot the
multiple graphs of the given functions and their derivatives on the interval [0, 5]. f x = x 2 +
2x + 10 and g x = sin x (P#116 – LECT#05)
%Differentiate the following functions using diff command.
%Moreover plot the multiple graphs of the given functions and their derivatives on the
interval [0, 5].
%f(x)=x^2+2x+10 and g(x)=sin(x)
clc
clear all
close all
syms x
f=x^2+2*x+10
g=sin(x)
f1=diff(f)
g1=diff(g)
x=0:0.1:5;
plot(x,(x.^2+2.*x+10),x,(2.*x + 2),x,sin(x),x,cos(x))
xlabel('x-axis')
ylabel('y-axis')
title('Graph of function and its derivatives using diff and plot')
grid
MIX_Q28: Plot the contour plots of the Sin(x) +Cos(y) where x range from −2𝜋 𝑡𝑜 2𝜋, y
range from 0 𝑡𝑜 4𝜋. Label the horizontal axis with x and vertical axis with y. Keep the title of
the graph “Contour Plot of Sin(X)+Cos(Y).
%Plot the contour plots of the Sin(x) +Cos(y)
%where x range from -2? to 2?, y range from 0 to 4?.
%Label the horizontal axis with x and vertical axis with y.
%Keep the title of the graph “Contour Plot of Sin(X)+Cos(Y).
clc
clear all
close all
MTH643 - INTRODUCTION TO MATLAB
From: ABU SULTAN
Page 11 of 22
Important and Solved Questions for Mid Term Exam
syms x y
[X,Y]=meshgrid(-2*pi:0.01:2*pi,0:0.01:4*pi);
contour(X,Y,sin(X)+cos(Y))
xlabel('x-axis');
ylabel('y-axis');
title('Contour Plot of Sin(X)+Cos(Y)')
grid
L07_Q29: Find the solution of the following initial value problem dy/dx = x 2 + y; y(1) = 3.
Moreover, plot the graph of the solution using ezplot command on the interval [0,2].
(P#187-189 - EXERCISE – LECT#07)
%Find the solution of the following initial value problem dy/dx = x2 + y; y(1) = 3.
%Moreover, plot the graph of the solution using ezplot command on the interval [0,2].
clc
clear all
close all
syms y
eq1='Dy=x^2+y';
y=dsolve(eq1,'y(1)=3','x')
ezplot(y,[0,2])
xlabel('x-axis')
ylabel('y-axis')
title('Solution of Initial Value Problem and ezplot of solution')
grid
L07_Q30: Find general solution of the following boundary value problem y′′ x + 8y ′ x +
2y x = cos (x). Then plot the solution curve on the interval [0,4] corresponding to the
following boundary condtions. y 0 = 4, y′ 1 = 5.
(P#215 - EXERCISE – LECT#07)
%Find general solution of the following boundary value problem y''x)+8y'(x)+2y(x)=cos(x).
%Then plot the solution curve on the interval [0,4]
%corresponding to the following boundary condtions. y(0)=4,y'(1)=5.
clc
clear all
close all
%syms x y
eq1='D2y+8*Dy+2*y=cos(x)';
icon='y(0)=4,Dy(1)=5';
y=dsolve(eq1,icon,'x')
ezplot(y,[0,4])
xlabel('x-axis')
ylabel('y-axis')
title('Solution of Boundary Value Problem and Solution Curve')
grid
L07_Q31: Plot the family of curves of the following differential equation on the interval
[0,3], with initial values y(0) = -3 : 3, dy/dx = x * y + 2 * x2.
(P#194-196 - EXERCISE – LECT#07)
%Plot the family of curves of the following differential equation on the interval [0,3].
%with initial values y(0) = -3 : 3, dy/dx = x * y + 2 * x2.
clc
clear all
MTH643 - INTRODUCTION TO MATLAB
From: ABU SULTAN
Page 12 of 22
Important and Solved Questions for Mid Term Exam
close all
eq1='Dy=x*y+2*x^2';
y=dsolve(eq1,'y(0)=c','x')
figure; hold on
for cval=-3:3
ezplot(subs(y,'c',cval),[0,3])
end
xlabel('x-axis')
ylabel('y-axis')
title('Family of Curves of DE')
grid
L07_Q32: Find the general solution of the following differential equation y ′ x = 2xy + 2x 3
. Also plot the family of solution curves on the interval [-4,4] of above differential equation
obtained using the following initial conditions y(0) = -3,-2-1,0,1,2,3.
(P#214 - EXERCISE – LECT#07)
%Find the general solution of the following differential equation y'(x)=2xy+2x^3.
%Also plot the family of solution curves on the interval [-4,4] of above differential equation
%obtained using the following initial conditions y(0) = -3,-2-1,0,1,2,3.
clc
clear all
close all
eq1='Dy=2*x*y+2*x^3';
y=dsolve(eq1,'y(0)=c','x')
figure; hold on
for cval=-3:3
ezplot(subs(y,'c',cval),[-4,4])
end
xlabel('x-axis')
ylabel('y-axis')
title('Family of Curves of DE')
grid
L07_Q35: Solve the following system of differential equations by using dsolve command.
x ′ t = x t + 2y t − z t
y′ t = x t + z t
z ′ t = 4x t − 4y t + 5z t
(P#204-205 - EXERCISE – LECT#07)
%Solve the following system of differential equations by using dsolve.
%x'(t)=x(t)+2y(t)-z(t)
%y'(t)=x(t)+z(t)
%z'(t)=4x(t)-4y(t)+5z(t)
eq1='Dx=x+2*y-z';
eq2='Dy=x+z';
eq3='Dz=4*x-4*y+5*z';
[x,y,z]=dsolve(eq1,eq2,eq3)
L08_Q40: Find Numerical solution of the following differential equation on the interval [0.1]
𝑑𝑦
using Euler’s Formula for n=10, 𝑑𝑥 = 𝑆𝑖𝑛 𝑥𝑦 , 𝑦 0 = 𝜋 (P#225-229 – LECT#08)
function [xvalues,yvalues]=euler(f,x0,y0,xn,n);
format long
x0=0;
y0=pi;
xn=1;
n=10;
x(1)=x0;
y(1)=y0;
xn=1;
dx=(xn-x0)/n;
for k=1:n
x(k+1)=x(k)+dx;
y(k+1)=y(k)+sin(x(k)*y(k))*dx;
end
xvalues=x'
yvalues=y'
plot(xvalues,yvalues)
L08_Q41: Find the numerical solution of the following differential equation on the interval
𝑑𝑦 𝑦 −𝑥
[0; 1] using Euler’s method for n = 20. 𝑑𝑥 = 𝑦 +𝑥 , 𝑦 0 = 0.5
(FOLLOW P#225-229 – LECT#08)
%Find the numerical solution of the following differential equation on the interval [0,1]
%using Euler’s method for n = 20. dy/dx=(y-x)/(y+x) ,y(0)=0.5
function [xvalues,yvalues]=euler(f,x0,y0,xn,n);
format long
x0=0;
y0=0.5;
xn=1;
n=20;
x(1)=x0;
y(1)=y0;
xn=1;
dx=(xn-x0)/n;
for k=1:n
x(k+1)=x(k)+dx;
y(k+1)=y(k)+((y(k)-x(k))/(y(k)+x(k)))*dx;
end
xvalues=x'
yvalues=y'
plot(xvalues,yvalues)
MTH643 - INTRODUCTION TO MATLAB
From: ABU SULTAN
Page 17 of 22
Important and Solved Questions for Mid Term Exam
L08_Q42: Find the numerical solution of the following differential equation on the interval
𝑑𝑦
[0; 1] using improved Euler’s Formula for n=10, 𝑑𝑥 = 𝑆𝑖𝑛 𝑥𝑦 , 𝑦 0 = 𝜋
(P#231-235 – LECT#08)
%Find the numerical solution of the following differential equation on the interval [0,1]
%using improved Euler’s Formula for n=10, dy/dx=Sin(xy),y(0)=?
function [xvalues,yvalues]=impeuler(f,x0,y0,xn,n);
format long
f=@(x,y)sin(x.*y);
x0=0;
y0=pi;
xn=1;
n=10;
x(1)=x0;
y(1)=y0;
xn=1;
dx=(xn-x0)/n;
for k=1:n
x(k+1)=x(k)+dx;
y(k+1)=y(k)+dx/2*(f(x(k),y(k))+f(x(k),y(k)+dx*y(k)));
end
xvalues=x'
yvalues=y'
plot(xvalues,yvalues)
L08_Q43: Find the numerical solution of the following differential equation on the interval
𝑑𝑦 𝑦 −𝑥
[0,1] using Improved Euler’s method for n = 20. 𝑑𝑥 = 𝑦 +𝑥 , 𝑦 0 = 0.5
(FOLLOW P#231-235 – LECT#08)
%Find the numerical solution of the following differential equation on the interval [0,1]
%using Improved Euler’s method for n = 20. dy/dx=(y-x)/(y+x) ,y(0)=0.5
function [xvalues,yvalues]=impeuler(f,x0,y0,xn,n);
format long
f=@(x,y)((y-x)/(y+x));
x0=0;
y0=1;
xn=0.5;
n=20;
x(1)=x0;
y(1)=y0;
xn=1;
dx=(xn-x0)/n;
for k=1:n
x(k+1)=x(k)+dx;
y(k+1)=y(k)+dx/2*(f(x(k),y(k))+f(x(k),y(k)+dx*y(k)));
end
xvalues=x'
yvalues=y'
plot(xvalues,yvalues)
MTH643 - INTRODUCTION TO MATLAB
From: ABU SULTAN
Page 18 of 22
Important and Solved Questions for Mid Term Exam
L08_Q44: Find the numerical solution of the following initial value problem on the interval
[0,1] using improved Euler’s method by taking step size ∆𝑡 = 0.01, Moreover, plot the graph
𝑑𝑦 𝑦−𝑡
of the approximated solution curve. 𝑑𝑡 = 𝑦+𝑡 , 𝑦 0 = 0.5
Note: The iterative form of the improved Euler’s method is given below;
∆𝑡 ′
𝑦𝑘+1 ≈ 𝑦𝑘 + 𝑦 + 𝑓(𝑡𝑘+1 , 𝑦𝑘 + ∆𝑡𝑦𝑘′ ), 𝑘 = 0,1,2, … … 𝑛 − 1
2 𝑘
(FOLLOW P#231-235 – LECT#08) –
(ABU SULTAN MID TERM Q#1 – 26 DEC 2019 – 12:00 pm)
%Find the numerical solution of the following initial value problem on the interval [0,1]
%using improved Euler’s method by taking step size ∆t=0.01,
%Moreover, plot the graph of the approximated solution curve. dy/dt=(y-t)/(y+t) ,y(0)=0.5
function [yvalues,tvalues]=impeuler(f,y0,t0,yn,n);
format long
f=@(y,t)((y-t)/(y+t));
y0=0;
t0=1;
yn=0.5;
n=20;
y(1)=y0;
t(1)=t0;
yn=1;
dt=0.01;
for k=1:n
y(k+1)=y(k)+dt;
t(k+1)=t(k)+dt/2*(f(y(k),t(k))+f(y(k),t(k)+dt*t(k)));
end
yvalues=y'
tvalues=t'
plot(yvalues,tvalues)
L08_Q45: Find the numerical solution of the following differential equation on the interval
dy
[0, 1] using Runge–Kutta method of fourth order for n=10, dx = Sin xy , y 0 = π
(SOLUTION P#237-244 - LECT#08)
%Find the numerical solution of the following differential equation on the interval [0,1]
%using Runge–Kutta method of fourth order for n=10, dy/dx=Sin(xy),y(0)=?
function [xvalues,yvalues]=RungeKutta(f,x0,y0,xn,n);
format long
f=@(x,y)sin(x.*y);
x0=0;
y0=pi;
xn=1;
n=10;
x(1)=x0;
y(1)=y0;
dx=(xn-x0)/n;
for k=1:n
x(k+1)=x(k)+dx;
k1=f(x(k),y(k));
k2=f(x(k)+dx/2,y(k)+dx/2*(k1));
k3=f(x(k)+dx/2,y(k)+dx/2*(k2));
k4=f(x(k+1),y(k)+dx+(k3));
MTH643 - INTRODUCTION TO MATLAB
From: ABU SULTAN
Page 19 of 22
Important and Solved Questions for Mid Term Exam
y(k+1)=y(k)+dx/6*(k1+2*k2+2*k3+k4);
end
xvalues=x'
yvalues=y'
plot(xvalues,yvalues)
title('Runge Kutta Method of order four');
grid;
xlabel('x-axis');
ylabel('y-axis');
𝑑𝑦
L08_Q46: = 𝐶𝑜𝑠 𝑥 + 𝑦 , 𝑛 = 10, 0,1 , 𝑦 0 = 𝑝𝑖. Solve by using Range-kutta method.
𝑑𝑥
(FOLLOW SOLUTION P#237-244 - LECT#08)
% dy/dx=Cos(x+y),n=10,[0,1],y(0)=pi
% Solve by using Range-kutta method.
function [xvalues,yvalues]=RungeKutta(f,x0,y0,xn,n);
format long
f=@(x,y)cos(x+y);
x0=0;
y0=pi;
xn=1;
n=10;
x(1)=x0;
y(1)=y0;
dx=(xn-x0)/n;
for k=1:n
x(k+1)=x(k)+dx;
k1=f(x(k),y(k));
k2=f(x(k)+dx/2,y(k)+dx/2*(k1));
k3=f(x(k)+dx/2,y(k)+dx/2*(k2));
k4=f(x(k+1),y(k)+dx+(k3));
y(k+1)=y(k)+dx/6*(k1+2*k2+2*k3+k4);
end
xvalues=x'
yvalues=y'
plot(xvalues,yvalues)
title('Runge Kutta Method of order four');
grid;
xlabel('x-axis');
ylabel('y-axis');
L08_Q47: Find the numerical solution of the following differential equation on the interval
𝑑𝑦 𝑦 −𝑥
[0,1] using Runge–Kutta method of fourth order for n = 20 𝑑𝑡 = 𝑦 +𝑥 , 𝑦 0 = 0.5
(FOLLOW SOLUTION P#237-244 - LECT#08)
% Find the numerical solution of the following differential equation on
% the interval [0, 1] using Runge–Kutta method of fourth order for n = 20
% dy/dt=(y-x)/(y+x) ,y(0)=0.5
function [xvalues,yvalues]=RungeKutta(f,x0,y0,xn,n);
format long
f=@(x,y)((y-x)/(y+x));
x0=0;
y0=0.5;
MTH643 - INTRODUCTION TO MATLAB
From: ABU SULTAN
Page 20 of 22
Important and Solved Questions for Mid Term Exam
xn=1;
n=20;
x(1)=x0;
y(1)=y0;
dx=(xn-x0)/n;
for k=1:n
x(k+1)=x(k)+dx;
k1=f(x(k),y(k));
k2=f(x(k)+dx/2,y(k)+dx/2*(k1));
k3=f(x(k)+dx/2,y(k)+dx/2*(k2));
k4=f(x(k+1),y(k)+dx+(k3));
y(k+1)=y(k)+dx/6*(k1+2*k2+2*k3+k4);
end
xvalues=x'
yvalues=y'
plot(xvalues,yvalues)
title('Runge Kutta Method of order four');
grid;
xlabel('x-axis');
ylabel('y-axis');
L08_Q48: Find Numerical solution of the following differential equation on the interval [0,1]
𝑑𝑦
using Taylor’s Formula for n=10, = 𝑆𝑖𝑛 𝑥𝑦 , 𝑦 0 = 𝜋
𝑑𝑥
(SOLUTION P#245-250 - LECT#08)
%rical solution of the following differential equation on the interval
%[0,1] using Taylor’s Formula for n=10, dy/dx=Sin(xy),y(0)=?
function [xvalues,yvalues]=TaylorOrder(f,x0,y0,xn,n);
format long
f=@(x,y)sin(x.*y);
x0=0;
y0=pi;
xn=1;
n=10;
x(1)=x0;
y(1)=y0;
dx=(xn-x0)/n;
for k=1:n
x(k+1)=x(k)+dx;
y(k+1)=y(k)+f(x(k),y(k))*dx+(y(k)*cos(x(k)*y(k))+x(k)*cos(x(k)*y(k))*f(x(k),y(k)))*(dx)^2
/2;
end
xvalues=x'
yvalues=y'
plot(xvalues,yvalues)
title('Taylor’s Method of Second Order');
grid;
xlabel('x-axis');
ylabel('y-axis');
MTH643 - INTRODUCTION TO MATLAB
From: ABU SULTAN
Page 21 of 22
Important and Solved Questions for Mid Term Exam
L08_Q49: Find the numerical solution of the following differential equation on the interval
dy y−x
[0,1] using Taylor’s method of second order for n = 20. dx = y+x , y 0 = 0.5
(FOLLOW SOLUTION P#245-250 - LECT#08)
%Find the numerical solution of the following differential equation on
% the interval [0,1] using Taylor’s method of second order for n = 20.
%dy/dx=(y-x)/(y+x) ,y(0)=0.5
function [xvalues,yvalues]=TaylorOrder(f,x0,y0,xn,n);
format long
f=@(x,y)((y-x)/(y+x));
x0=0;
y0=0.5;
xn=1;
n=20;
x(1)=x0;
y(1)=y0;
dx=(xn-x0)/n;
for k=1:n
x(k+1)=x(k)+dx;
y(k+1)=y(k)+f(x(k),y(k))*dx+(y(k)*cos(x(k)*y(k))+x(k)*cos(x(k)*y(k))*f(x(k),y(k)))*(dx)^2
/2;
end
xvalues=x'
yvalues=y'
plot(xvalues,yvalues)
title('Taylor’s Method of Second Order');
grid;
xlabel('x-axis');
ylabel('y-axis');
𝑑𝑦
L08_Q50: Solve by Taylor method 𝑑𝑥 = 𝐶𝑜𝑠 𝑥𝑦 , 𝑦 0 = 1, ℎ𝑒𝑟𝑒 𝑛 = 10 𝑜𝑟𝑑𝑒𝑟.
(FOLLOW SOLUTION P#245-250 - LECT#08)
%Solve by Taylor method dy/dx=Cos(xy),y(0)=1,here n=10 order
function [xvalues,yvalues]=TaylorOrder(f,x0,y0,xn,n);
format long
f=@(x,y)cos(x.*y);
x0=0;
y0=1;
xn=1;
n=10;
x(1)=x0;
y(1)=y0;
dx=(xn-x0)/n;
for k=1:n
x(k+1)=x(k)+dx;
y(k+1)=y(k)+f(x(k),y(k))*dx+(y(k)*cos(x(k)*y(k))+x(k)*cos(x(k)*y(k))*f(x(k),y(k)))*(dx)^2
/2;
end
xvalues=x'
yvalues=y'
plot(xvalues,yvalues)
title('Taylor’s Method of Second Order');
MTH643 - INTRODUCTION TO MATLAB
From: ABU SULTAN
Page 22 of 22
Important and Solved Questions for Mid Term Exam
grid;
xlabel('x-axis');
ylabel('y-axis');
MIX_Q51: Find the solution of the following second order differential equation
𝑦 ′′ 𝑡 + 8𝑦 ′ 𝑡 + 2𝑦 𝑥 = 𝐶𝑜𝑠(𝑥). (PAST PAPERS)
%Find the solution of the following second order differential equation
%y''(t)+8y'(t)+2y(x)=Cos(x).
clc
clear all
close all
eq1='D2y+8*Dy+2*y(x)=cos(x)'
dsolve(eq1,'t')
MIX_Q52: Plto the contour plots of the expression z=x*exp(-x^2-y^2) range is -2<x<2 and -
3<y<3. Label teh horizontal axis with x and vertical axis with y. keep the title of graph
contour plots of the expression.
(ABU SULTAN MID TERM Q#2 – 26 DEC 2019 – 12:00 pm)
___________The End___________