Skip to content
VUBuddy

MTH643 Final Term Lectures 8-15 by Yasir

Introduction to MATLAB PDF 846 KBUpdated 23 Oct 2026
Final term past paper MTH643 Final Term Lectures 8-15 by Yasir PDF 846 KB
vubuddy.com
Type
Final term past paper
Course
MTH643 Introduction to MATLAB
Size
PDF 846 KB
Updated
23 Oct 2026
Free download 0 of 3 used today
Download free · 846 KB

You get 3 free files a day on the website. Add the Chrome extension for 30 a day.

Unlimited downloads in the Android app Get it free on Google Play

Preview text

Text from pages 1, 2, 3 of 4. Download the file for all 4 pages.

Mth-643 Final term lect8to15(yasir)

Matrix Algebra:

clear all clc Rowvector=[1,2,3] Columnvector=[1;2;3] threeby3matrix=[1,2,3;3,4,5;7,6,8] transpose(threeby3matrix)

%Built in Functions

A=pascal(3) %symmetric matix A^T=A B=magic(3) %antisymmetric F=ones(3) %matrix of ones G=eye(3) %Identiy matrix H=zeros(2) % Zero matrix

%diagonal matrix from a vector

v=[2,3,4]; D=diag(v) E=diag(A) % diagonal enteries elehda sy vector mn store

%add scalar quantity to each element of

vector

J=[2,4,5;6,7,1;1,1,1]; K=3+J

%Matrix Multiplication 1st n=2nd m

syms x y T=[1,2;7,5]; X=[x;y] Z=T*X

Matrix Algebra:

%Matrix Multiplication using for Loop

matrix1=[1;2;3]; matrix2=[1,4,5]; m=3; n=3; for i=1:m for j=1:n c(i,j)=matrix1(i,:)*matrix2(:,j)

%Matrix Multiplication Simple method

A=[1;2;3]; %follow multiplication rule B=[1,2,3]; C=A*B

% Multiply Individual Entries

A=[1,2,3];B=[4,3,2]; C=A.*B

%divide Idividual entries

A=[2,4,6]; B=[6,8,6]; C=A./B %A oper B nechy D=A.\B %B oper A nechy

%Eigen Values

A=[1,2,3;2,3,5;4,3,2]; eig(A); [V,D]=eig(A)

%Reduced Row Echelon Form

A=[1,2,3;1,3,4;2,5,7]; B=rref(A)

System of Linear Equations:

%x+2y+4z=1,%3x+2y+4z=2,%2x+4y+5z=5

A=[1,2,4;3,2,4;2,4,5]; X=[x;y;z]; B=[1;2;5]; X=inv(A)*B; %1st method X=vpa(A\B); %2nd method X=mldivide(A,B); %3rd method eqn1='x+2*y+4*z=1'; %4th method eqn2='3*x+2*y+4*z=2'; eqn3='2*x+4*y+5*z=5'; sol=solve(eqn1,eqn2,eqn3) X=double([sol.x,sol.y,sol.z])'

Matrix Equation of Sylvester Type:

AX+BX=C

A=[1,2,4;5,6,7;2,3,5]; B=[1,3,4;5,2,3;1,4,6]; C=[1,3,9;3,6,8;2,3,5]; X=sylvester(A,B,C)

For Loop: Example(1)

%we want to add each element of vector v=1:10; sum=0; for i=1:length(v) sum=sum+i

fprintf('sum of all enteries of vector= %d\n',sum)

For Loop: Example(2)

%we want to add 1st 30 odd numbers v=1:60; sum=0; for i=1:2:length(v) sum=sum+i

fprintf('sum of 1st 30 odd enteries of vector= %d\n',sum)

For Loop: Example(3)

%we want to add 1st 30 even numbers v=1:60; sum=0; for i=1:length(v) if(rem(i,2)==0) sum=sum+i;

fprintf('sum of 1st 30 even enteries of vector= %d\n',sum)

For Loop: Example(4)

%Calculate factorial num=input(' Enter a number= '); fact=1; while num<0 disp(' Enter a positive Number') num=input(' Enter a number= ');

if num>0 for i=1:num fact=fact*i; fprintf(' The factorial of %d is= %d\n',num,fact)

Nested For Loop: Example(5)

%print a patteren rows=input(' How many rows do you want to print: '); for R=1:rows for S=1:R fprintf('*')

fprintf('\n')

Differentiation and Integration:

%y=x^3

f=x^3; df=diff(f,x,2); %differentiation i=int(f,0,2); %Integration %sin(2x) g=sin(2*x); dg=diff(g,x); integ=int(g,x,0,pi/2); %x^2y^3z^2 h=x^2*y^3*z^2; dh=diff(h,z); Ih=int(int(int(h,0,pi),0,pi/2),0,1)

Solution Of ODES: Example(1)

%dy/dt=ty %y(0)=1 syms y(t) ode=diff(y,t)==t*y; cond=y(0)==1; soly(t)=dsolve(ode,cond)

Solution Of ODES: Example(2)

%dy/dt+4y(t)=exp(-t) %y(0)=1 syms y(t) ode=diff(y,t)+4*y(t)== exp(-t); cond=y(0)==1; soly(t)=dsolve(ode,cond)

Solution Of ODES: Example(3)

%d^2y/dt^2=cos(2t)-y %y(0)=1 %y'(0)=0 syms y(t) Dy=diff(y); ode=diff(y,t,2)==cos(2*t)-y; cond1=y(0)==1; cond2=Dy(0)==0; cond=[cond1 cond2]; soly(t)=dsolve(ode,cond) simplesol=simplify(soly(t))

Solution Of ODES: Example(4)

%d^3u/dx^3=u %u(0)=1 %u'(0)=-1 %u''(0)=pi syms u(x) Du=diff(u); D2u=diff(u,2); ode=diff(u,x,3)==u; cond1=u(0)==1; cond2=Du(0)==-1; cond3=D2u(0)==pi; cond=[cond1 cond2 cond3]; solu(x)=dsolve(ode,cond) simplesolu(x)=simplify(solu(x))

System Of ODES: Example(1)

%du/dt=3u+4v %dv/dt=-4u+3v syms u(t) v(t) ode1=diff(u,t)==3*u+4*v; ode2=diff(v,t)==-4*u+3*v; ode=[ode1 ode2]; sol=dsolve(ode); sol.u; sol.v; %OR [usol vsol]=dsolve(ode)

Ode45:Example(1)

%F=3exp(-t) %tspan=[0,5] %x0=0 %F=3exp(-t) %tspan=[0,5] %y0=0 syms t

tspan=[0,5]; f0=0; [t,F]=ode45(F,tspan,f0) plot(t,F)

Ode45:Example(2)

%y'=2t %tspan=[0,5] %y0=0

tspan=[0,5]; y0=0; [t y]=ode45(yprime,tspan,y0) plot(t,y) xlabel('t-axis');

title(' Graph of yprime=2t')

Ode45:Example(3)

%u'=2u+v+5w+exp(-2t) %v'=-3u-2v-8w+2exp(-2t)-cos(3t) %w'=3u+3v+2w+cos(3t) %tspan=[0,5] %u(0)=1 %v(0)=-1 %w(0)=0 clear all clc close all syms u v w t uprime=2*u+v+5*w+exp(-2*t); vprime=-3*u-2*v-8*w+2*exp(-2*t)-cos(3*t); wprime=3*u+3*v+2*w+cos(3*t); tspan=[0,5]; u0=1; v0=-1; w0=0; cond=[u0,v0,w0];

3*x(1)-2*x(2)-8*x(3)+2*exp(-2*t)- cos(3*t);3*x(1)+3*x(2)+2*x(3)+cos(3*t)] [t,x]=ode45(ode,tspan,cond) plot(t,x) xlabel('X-axis')

title(' Plot of Solution Curves')

Solution of differential equation using · numerical method: · Euler’s Method:

%Euler Method %y(x+h)=y(x)+hy'(x) %y(x+h)=y(x)+hf(x,y) %f(x,y)=x/y %Euler Method %y(x+h)=y(x)+hf(x,y) %f(x,y)=x/y %a=0 %b=2 %y0=1 %x0=0 %n=10 clear all clc

a=0; b=2; y0=1; x0=0; n=10; h=(b-a)/n; x(1)=x0; y(1)=y0; for j=1:n y(j+1)=y(j)+h*f(x(j),y(j)); x(j+1)=x(j)+h;

plot(x,y) hold on syms Y(X) ode=diff(Y,X)==X/Y; cond=Y(0)==1; S=dsolve(ode,cond) F=matlabFunction(S); fplot(F,[0,2],'r',2) hold off

Trapezoidal Rule:

𝑏

𝑎 ∆𝑥 𝑛−1

𝑗=1 %Trapezoidal Rule %find area of curve x^2 %a=0,%b=2, %n=3

a=0; b=2; n=3; h=(b-a)/n; sum=0; x(1)=1; for i=1:n-1 x(i)=a+i*h; y(i)=f(x(i)); sum=sum+y(i);

Area=h/2*[f(a)+f(b)+2*sum] %find area of curve exp(x^2)

a=0; b=2; n=20; h=(b-a)/n; sum=0; x(1)=1; for i=1:n-1 x(i)=a+i*h; y(i)=f(x(i)); sum=sum+y(i);

Area=h/2*[f(a)+f(b)+2*sum]

Simpson’s 1/3 Rule:

𝑏

𝑎 ℎ 𝑛 2−1

𝑗=1 𝑛

𝑗=1 %Simpson1by3rule %y=x^3 %a=0, %b=2, %n=10, clear all clc

a=0; b=2; n=10; % n must even h=(b-a)/n; if (rem(n,2)~=0) fprintf('\n Enter Valid n'); n=input('\n Enter n as Even');

Simpson’s 3/8 Rule:

𝑏

𝑎 3ℎ 𝑛 3−1

𝑗=1

𝑛−1

%Simpson 3/8 rule %exp(2x)sin(6x) clear all clc

a=0; b=4; n=12; % n must be multiple of 3 if (rem(n,3)~=0)

All MTH643 files →
MTH643 Introduction to MATLAB All MTH643 handouts, past papers, quizzes and MCQs in one place. Open course page

Get more out of VUBuddy, for free

Android app Free on Google Play
  • Unlimited downloads
  • The explanation behind every MCQ answer
  • Study offline on slow data
Get the Android app
Chrome extension Free on the Chrome Web Store
  • 30 downloads a day instead of 3
  • Sign in and buy Pro Semester here
Add to Chrome

One Pro Semester membership works in both the extension and the app.