Professional Documents
Culture Documents
Display Window
•Command Window
•Graphics Window
•Edit Window
c=
4
S.K. Dwivedy IITG/MECH/SKD_ME101_MATLA 11
7 B BASICS_MARCH 2023
Matrix Manipulation
This demo examines some basic matrix manipulations in
MATLAB. We start by creating a magic square and
assigning it to the variable A
>>A = magic(3)
A=
8 1 6
3 5 7
4 9 2
Here's how to add 2 to each element of A.
S.K. Dwivedy IITG/MECH/SKD_ME101_MATLA 12
B BASICS_MARCH 2023
Note that MATLAB requires no special handling of
matrix math.
>>A+2
ans =
10 3 8
5 7 9
6 11 4
8 3 4
1 5 9
6 7 2
S.K. Dwivedy
30 30 30 IITG/MECH/SKD_ME101_MATLA 15
B BASICS_MARCH 2023
We can also multiply each element of A with
its corresponding element of B by using the .*
operator.
>>A.*B 8 1 6 2 2 2
ans = A = 3 5 7 , B = 2 2 2
4 9 2 2 2 2
16 2 12
6 10 14
8 18 4
abs(x)
sqrt(x)
ans
pi
eps
inf
i
j
NaN
clock
date
who whos
what clear
clear x y z clear all
mlock fun munlock fun
clc home
clf pwd
cd dir
ls path
edit path copyfile
S.K. Dwivedy mkdir IITG/MECH/SKD_ME101_MATLA 23
B BASICS_MARCH 2023
Plotting simple graphs
t=0:0.1:2*pi;
y = exp(t/10).*sin(t);
plot(t,y);
xlabel('Time (t) in second')
ylabel('Response amplitude in mm')
%print resp_amp.pdf -dpdf
fplot('exp(-0.1*t).*sin(t)',[0,30])
xlabel('time')
ylabel('f(x)=e^{0.1 t}sin(t)')
title('A function plotted with (fplot')
print f2.jpg -djpeg
g=inline('x^2');
ezplot(g)
title('A plot using ezplot command')
print f3.jpg -djpeg
x='t1.*cos(2.*pi*t1)';
y='t1.*sin(2*pi*t1)';
z='t1';
ezplot3(x,y,z)
title('Figure using ezplot3')
print f5.jpg -djpeg
Z=inline('cos(2*x).*cos(y).*exp(-sqrt((x.^2+y.^2)/4))');
ezcontour(Z)
print f6.jpg -djpeg
ezcontourf(Z)
ezsurfc(Z,[-3,3,-3,3])
print resp9.jpg -djpeg
>t=linspace(0,20,500);
>y=2*cos(t)+cos(2*t);
>ydot=-2*sin(t)-(2)*sin((2)*t);
>subplot(3,2,1)
>plot(t,y);
>grid on
>subplot(3,2,2)
>plot(t,ydot)
>grid on
>subplot(3,1,2)
>plot(y,ydot)
>grid on
>subplot(3,1,3)
>plot(y,ydot)
>grid on
1 0 0
clc 1 0 0 0
1 0
a=[1 0 0;0 1 0;0 0 1] a = 0 1 0 a1 =
0 0 1
u=[4 5 7]; 0 0 1
v=[7; 8; 5] 4 5 7
a1=[a;u] 1 0 0 7
b=[a v] b = 0 1 0 8
c=[] 1 0 1 5
d=zeros(2,4) 0 0 0 0
d =
S.K. Dwivedy 0 0
IITG/MECH/SKD_ME101_MATLA
0 0 38
B BASICS_MARCH 2023
v=
7
8
5
a1 =
1 0 0
0 1 0
0 0 1
4 5 7
Hadamard,
hankel,
hilb,
invhilb, >Help specmat
kron,
pascal,
Toeplitz,
vander,
magic
S.K. Dwivedy IITG/MECH/SKD_ME101_MATLA 44
B BASICS_MARCH 2023
Some functions
>> linspace(0,10,4)
ans =
0 3.3333 6.6667 10.0000
>> logspace(0,3,4)
ans =
1 10 100 1000
Script file
Write all valid matlab command in a file and save it as
.m file (e.g., amp.m).
All the variables are global variables
Results obtained from executing the script file are left
in the workspace.
Execute it by typing the file name (e.g., >amp)
A function file is also an m-file, like script file, except that the
variables are local (name should be same as the function name)
feval=Function evaluation
>> f =inline('r^3+2*r-10');
>> Output=fzero(f,2)
Output =
1.8474
function f= bar1(r)
f =r^3+2*r-10;
>>a=fzero(@bar1,2)
a=
1.8474
S.K. Dwivedy IITG/MECH/SKD_ME101_MATLA 50
B BASICS_MARCH 2023
For loops
sum=0.0;
sum=0.0;
for m=1:1:10
for m=1:100
sum=sum+m;
sum=sum+m;
end
end
sum=
sum=
55
5050
clc;
v=1; num=1; i=1;
while num<300
num=3^i; v=[v;num]; i=i+1;
end
>> v'
ans =
1 3 9 27 81 243 729
S.K. Dwivedy IITG/MECH/SKD_ME101_MATLA 52
B BASICS_MARCH 2023
If-elseif-else statement
clc;
i=4;j=20;
if i>5 k=i;
elseif (i>1)&(j==20) k=5*i+j;
else k=1;
end
k
40
clc;
A=[1 2 3; 2 5 7; 7 8 3] % solve with Gaussian elimination
>>x = cr =rref ( C )
• Double integration
Integral=dblquad(‘fun’, xmin,xmax,ymin,ymax,tol)
F=inline(‘1-5*x^y’);
A=dblquad(F,0,2,-1,1)
S.K. Dwivedy IITG/MECH/SKD_ME101_MATLA 56
B BASICS_MARCH 2023
Integration
• For analytical integral
U=int(@fun,lowerlimit,upperlimit),
%------for example-----------
>> syms x
>> Z=exp(x)*cos(x)*x^2;
>> P=int(Z)
P=
(1/2*x^2-1/2)*exp(x)*cos(x)-(-
1/2*x^2+x-1/2)*exp(x)*sin(x)
>> P=int(Z,0,1)
P=
1/2
S.K. Dwivedy IITG/MECH/SKD_ME101_MATLA 57
B BASICS_MARCH 2023
•For numerical integration
Syntax
q = quad or quadl or quad8(fun,a,b)
q = quad or quadl or quad8(fun,a,b,tol)
%------------------Example-----------
>> F = @(x)1./(x.^3-2*x-5); >> F = inline('1./(x.^3-2*x-5)');
>> Q = quad(F,0,2) or >> Q = quad(F,0,2)
Q= Q=
-0.4605 -0.4605
Double integration
Syntax
q = dblquad(fun,xmin,xmax,ymin,ymax)
%------------------------ Example--------------------------
>> F = @(x,y)y*sin(x)+x*cos(y); >> F =inline('y*sin(x)+x*cos(y)');
>> Q = dblquad(F,pi,2*pi,0,pi) >> Q = dblquad(F,pi,2*pi,0,pi)
Q= Q=
-9.8696 -9.8696
>> >>
S.K. Dwivedy IITG/MECH/SKD_ME101_MATLA 58
B BASICS_MARCH 2023
Differentiation
Y = diff(Funtionname,variablename,numberofoder)
%..........................For Example……………………….
>> syms x y
>> syms x
>> Z=cos(x)*sin(y);
>> Z=cos(x);
>> Y=diff(Z,x) >> P=diff(Z,x)
Y= P=
-sin(x) -sin(x)*sin(y)
Syntex
[t,Y] = solver(odefun,tspan,y0)
[t,Y] = solver(odefun,tspan,y0,options)
sol = solver(odefun,[t0 tf],y0...)
where solver is one of ode45, ode23, ode113, ode15s, ode23s, ode23t, or ode23tb.
#------------------For example-----------------How to solve the following ordinary
equation
This example explains and illustrates the steps you need to solve an initial value
ODE problem:
1.Rewrite the problem as a system of first-order ODEs. By substituting