MTH643 Final Term Solved
MTH643 Final Term Solved
K HAFEEZ
MTH643 current papers
Question No.1
Find the numerical solution of the following equation using ode45
command and plot the curve on [0 2].
y’(x)=y-x2+1 , y(0)=0.5
Solution
clc
clear all
close all
yprime=@(x,y)y-x^2+1;
xspan=[0,2];
y0=0.5;
[x,y]=ode45(yprime,xspan,y0);
plot(x,y)
Question No.2
Find the numerical solution of the following equation using ode45
command and plot the curve on [0 2].
y’(x)=y-x2+1 , y(0)=0.5;0.3;3
Solution
clc
clear all
close all
yprime=@(x,y)y-x^2+1;
xspan=[0,2];
y0=0.5:0.3:3;
[x,y]=ode45(yprime,xspan,y0);
plot(x,y)
Question No.3
Find the numerical solution of the following equation using ode45 command and
plot the curve on x=0:0.2:2
y’(x)=y-x2+1 , y(0)=0.5
Solution
clc
clear all
close all
yprime=@(x,y)y-x^2+1;
xspan=[0:0.2:2];
y0=0.5;
[x,y]=ode45(yprime,xspan,y0);
plot(x,y)
Question No.4
Write a for loop to add first 20 numbers which are divisible by 5.
Solution
clc
clear all
close all
sum=0;
for k=5:5:100;
sum=sum+k;
end
disp(sum)
Question No.5
Write a program which takes a value from user and then using for loop display all
of its divisors.
Solution
clc
clear all
close all
x=input('enter a number:')
i=x;
for j=1:i;
if mod(x,j)==0
disp(j)
end
end
Question No.6
Numerically approximate the solution of the following ode dy/dt=2t use time
interval [0 5] and initial condition is y(0)=0 by using ode45 command.
Solution
clc
clear all
close all
f=@(t,y)2*t
tspan=[0 5];
y0=0;
[t y]=ode45(f,tspan,y0)
plot(t,y)
Question No.7
Write a function with name ‘myfunction’ which takes any two number as input and
returns the greater number.
Solution
function [x,y]=myfunction(n1,n2)
if n1>n2
fprintf('The max number is n1')
else
fprintf('The max number is n2')
end
end
Question No.8
Write a function with name ‘newfunction’ which takes any two number as input
and returns the highest common factor of the number.
Solution
function [HCF]=newfunction(n1,n2)
for i=1:1:(n1+n2);
if mod(n1,i)==0 && mod(n2,i)==0
HCF=i;
end
end
Question No.9
Write a program which prints all the numbers divisible by 3 and 5 for a given
number.
Solution
clc
clear all
close all
x=input('enter a number:')
n=x;
for i=1:n;
if mod(i,3)==0 && mod(i,5)==0
disp(i)
end
end
Question No.10
Write a for loop which adds first 30 odd numbers.
Solution
clc
clear alll
close all
k=1:60;
sum=0;
for i=1:2:length(k);
sum=sum+i;
end
disp(sum)
Question No.11
Write a function name cube takes a number as an input if the number is even return
cube if odd then return square.
Solution
function [cubes square]=cube(n)
if mod(n,2)==1
fprintf('square')
else
fprintf('cube')
end
end
Question No.12
Find sum and write program
𝑛
3 𝑘
∑( )
𝑥
𝑘=1
Solution
clc
clear all
close all
n=1:12;
x=4;
sum=0;
for k=1:length(n);
sum=sum+(3/x)^k;
end
disp(sum)
Question No.13
Solve the newton equation x3+x by using for loop when x0=2 and n=3.
Solution
clc
clear all
close all
f=@(x)x^3+x
df=@(x)3*x^2+1
x0=2;
n=3;
x=x0;
for i=1:n;
x=x-f(x)/df(x)
end
Question No.14
Solve the newton equation x3+x by using while loop when x0=2 and n=3.
Solution
clc
clear all
close all
f=@(x)x^3+x
df=@(x)3*x^2+1
x0=2;
n=3;
x=x0;
tol=10^-4
while abs(f(x))>tol
x=x-f(x)/df(x)
end
fprintf('Root: %o.15 f\n',x)
Question No.15
Write a function with the name ‘practisefunction’ which takes any three random
variables as an input and returns the number in ascending order.
Solution
function [Ascending]=practisefunction(n1,n2,n3)
n1=input('enter the 1st number:')
n2=input('enter the 2nd number:')
n3=input('enter the 3rd number:')
A=[n1,n2,n3]
Ascendingorder=sort(A)
end
Question No.16
Find factorial of a number by using while loop.
Solution
clc
clear all
close all
n=input('enter a number:')
i=n;
fact=1;
if i==0
fact=1;
elseif i<0
fprintf('the number you are entered is nagative')
end
while i>0
fact=fact*i;
i=i-1;
end
disp(fact)
Question No.17
Find factorial of a number by using for loop.
Solution
clc
clear all
close all
n=input('enter a number:')
fact=1;
for i=1:n;
fact=fact*i;
end
disp(fact)
Question No.18
write a function with name examquestions which take any two number as an input
and return the sum and product of those number as output.
Solution
function [x y]=examquestions(x,y)
sum=x+y
product=x*y
end
Question No.19
Use while loop to calculate the factorial of 10.
Solution
clc
clear all
close all
n=10;
i=n;
fact=1;
while i>0
fact=fact*i;
i=i-1;
end
disp(fact)
Question No.20
Numerically solve the following equation by using ode45 command
𝑑𝑦 𝑦 2
= + when y(0)=1
𝑑𝑥 𝑡2 𝑦
Solution
clc
clear all
close all
f=@(t,y)t/y^2+2/t
tspan=[2 -2];
y0=1;
[t y]=ode45(f,tspan,y0)
plot(t,y)
Question No.21
1/3 8/4 4
3/11 2/5 4/7
A=[ ] and B=[2/9 3/7 9/6]
2/3 4/5 6/5
6 7/4 5/7
Then find
i) invA
ii) A*B
Solution
clc
clear all
close all
A=sym([3/11 2/5 4/7;2/3 4/5 6/5])
B=sym([1/3 8/4 4;2/9 3/7 9/6;6 7/4 5/7])
C=inv(B)
D=sym(A*B)
Question No.22
2x+y+z=2
-x+y-z=3
x+2y+3z=-10
Solution
clc
clear all
close all
syms x y z
A=[2,1,1;-1,1,-1;1,2,3]
B=[2;3;-10]
X=[x;y;z]
%AX=B%
C=rref(A,B)
Question No.23
y(x)=√1 + 𝑥 3 solve the equation using simpson’s 3/8 rule interval [1 5] and keep
step size 12.
Solution
clc
clear all
close all
f=@(x)sqrt(1+x^3)
a=1;
b=5;
n=12;
h=(b-a)/n;
if rem(n,3)~=0
fprintf('enter valid n')
input('enter n as the multiple of 3')
end
sum1=0;
sum2=0;
for i=1:n-1;
x(i)=a+i*h;
if rem(i,3)==0
sum1=sum1+f(x(i));
else
sum2=sum2+f(x(i));
end
end
Area=3*h/8*(f(a)+f(b)+2*sum1+3*sum2)
Question No.24
𝑑2 𝑦 𝑑𝑦
+2 + 5𝑦 = 7 and y(0)=1 and y’(0)=0
𝑑𝑡 2 𝑑𝑡
Solution
clc
clear all
close all
syms y(t)
Dy=diff(y,t)
ode=diff(y,t,2)+2*diff(y,t)+5*y==7
cond1=y(0)==1
cond2=Dy(0)==0
cond=[cond1,cond2]
sol=dsolve(ode,cond)
Question No.25
Solve x-2sin(x2)=0 by using bisection method perform 10 iterations to find the root between 0.4
and 0.6.
Solution
clc
clear all
close all
f=@(x)x-2*sin(x^2)
a=0.4;
b=0.6;
n=10;
if f(a)*f(b)>0
fprintf('there is no change of sign:')
return
end
tol=10^-4
disp('iteration number a b c')
i=1;
while (abs(a-b)>=tol)
c=(a+b)/2;
if f(c)==0
fprintf('root of the function is %f:',c)
return
end
fprintf('%2i \t %f \t %f \t %f \n',i,a,b,c)
if f(a)*f(c)>0
a=c;
else
b=c;
i=i+1;
end
end
m=(a+b)/2
fprintf('root lies at c=%f \n',m)
Question No.26
Write a function with name ‘circlecalculation’which take the circle of radius as
input and circf as output.
Solution
function [circf]=circlecalculation(radius)
radius=input('enter the radius of circle:')
circf=2*pi*radius
end
Question No.27
y(x)=√1 + 𝑥 3 solve the equation using simpson’s 1/3rule interval [1 5] and keep
step size 10.
Solution
clc
clear all
close all
f=@(x)sqrt(1+x^3)
a=1;
b=5;
n=12;
h=(b-a)/n;
if rem(n,2)~=0
fprintf('enter valid n')
input('enter n as the even multiple')
end
sum1=0;
sum2=0;
for i=1:n-1;
x(i)=a+i*h;
if rem(i,2)==0
sum1=sum1+f(x(i));
else
sum2=sum2+f(x(i));
end
end
Area=h/3*(f(a)+f(b)+2*sum1+4*sum2)
Question No.28
1/3 8/4 4
3/11 2/5 4/7
A=[ ] and B=[2/9 3/7 9/6]
2/3 4/5 6/5
6 7/4 5/7
Then find
Solution
clc
clear all
close all
A=sym([3/11 2/5 4/7;2/3 4/5 6/5])
B=sym([1/3 8/4 4;2/9 3/7 9/6;6 7/4 5/7])
SumA=sum(A(:,2))
SumB=sum(B(3,:))
Question No.29
Solve the following equation x2/2 by using trapezoidal rule in the interval [4 6] by
using step size 10.
Solution
clc
clear all
close all
f=@(x)x^2/2
a=4;
b=6;
n=10;
h=(b-a)/n;
sum=0;
for i=1:n-1;
x(i)=a+i*h;
y(i)=f(x(i));
sum=sum+y(i);
end
Area=h/2*(f(a)+f(b)+2*sum)
Question No.30
Solve the following equation x2/2 by using reimann sum in the interval [4 6] by
using step size 10.
Solution
clc
clear all
close all
f=@(x)x^2/2
a=4;
b=6;
n=10;
dx=(b-a)/n;
value=0;
for i=1:n;
c=a+i*dx;
value=value+f(c);
end
Area=value*dx
Question No.31
Find the value of X if
1 3 −2
A=[ ] and B=[ ]
2 4 3
Solution
clc
clear all
close all
A=[1,3;2,4]
B=[-2;3]
% As we know that
% AX=B
% X=inv(A)*B
C=inv(A)
X=C*B
Question No.32
Solve the system of linear equation by using solve command.
3x+2y+x=1
y=2
x+2y=3
Solution
clc
clear all
close all
A='3*x+2*y+z=1'
B='y=2'
C='x+2*y=3'
sol=solve(A,B,C)
Answer=double([sol.x,sol.y,sol.z])
Question No.33
Find the eigen values and eigen vector of the following matrix.
3 1 4
A=[7 8 9]
3 1 3
Solution
clc
clear all
close all
A=[3,1,4;7,8,9;3,1,3]
V=eig(A)
[u v]=eig(A)