math lab Flashcards
when is semicolon used
comment symbol
end of statement and when you want to hide the Matlab output for a expression
x=5; y = x+5 x=5 is not shown
% symbol
Dealing with Matrices and Arrays
2-D and 3-D Plotting and graphics
Linear Algebra
Algebraic Equations
Statistics
Data Analysis
Calculus and Differential Equations
Numerical Calculations
Integration
Transforms
Curve Fitting
Special Functions
MATLAB difference compared to other programs
While other programming languages mostly work with numbers one at a time, MATLAB is designed to operate primarily on whole matrices and arrays.
addition, subtraction, multiplication left and right division, exponentiation
+, -, *, /(5/3) (3\5) same value ^
special variables and constants
ans: most recent answer
eps: floating-point representation
I,j (imaginary unit root of -1)
inf - infinity
NaN (undefined numerical not a number)
pi = pi
what is who command and whos command
displays all the variable names we use
whos : gives size, bytes class,
clear x/var name
clear
clc
print in matlab
it will delete x
deletes all variables
clears all text from the command window
print in matlab
fprintf
Command Window
MATHLAB editor
workspace
command history
command directory
Command Window
the main area where commands can be entered at the command line. It is indicated by the command prompt (»)
MATLAB Editor. This is where you can write, edit, and save your scripts and functions.
commands can be done there and
command window: where all the editing happens variable assignment happens
workspace:
The workspace shows all the variables created and/or imported from files.
command history:
This panel shows or return commands that are entered at the command line.
command directory:
View Folders and m-files
format short
format short e
format long
format bank
format rat
displays numbers with four decimal places
format short
7+10/3+5^1.2
x=17.232
format shorte
the command allows displaying in exponential form with 4 decimal places plus the exponent
and = 2.2922e+01
format long
15 digits after the decimal point
format bank
rounds number to 2 decimal places
format rat
gives closest rational expression resulting from a calculation
format rat
4.678*4.9
= 34177/1491
two types of vectors
matrix
TO DISPLAY COLUMN VECTOR
row vectors and column vectors
row vector:
X =[7,8,9,10]
X= 7 8 9 10
column vector
X = [7; 8; 9; 10]
7
8
9
10
MATRIX
2D array numbers,
A = [1 2 3 4; 4 5 6 7; 7 8 9 10 ]
%same number of elements need to be present
1 2 3 4
4 5 6 7
7 8 9 10
disp(a)
elementary math built
trigonometry function
other functions
sqrt(x), n*nthroot(x,n) =>
real nth root of a real number 25 has 2 roots
exp(x), abs(x), log(x), log10(x), factorial(x)
trigonometry function
sin(x) => radians, sind(x) => degree
cos(x), cosd(x), tan(x), tand(x)
cot(x), cotd(x)
inverse
asin(x), asind(x), acos(x), acosd(x)
other function
round(x) (nearest integer), fix(x)=> Round towards 0, => chops fraction part to whole part
ceil(x) => round towards + infinity
ceil(27/2) => 14
floor(x) => rounds ‘-‘ infinity
floor(27/2) => 13
rem(x,y) remainder after x/y
example question y=e^a*sin(x) + 10root(y)
if a=5, x=2, y=8
a=5;
x=2;
y=8;
z=exp(a)sin(x)+10sqrt(y)
z=2.829039804
special type of arrays
four types of arrays are:
zeros(), +> zero array zeros(5) => 5 by 5
eye(x,y) => 1 diagonal towards left to right
ones(x,y), => pure ones
rand() => random num in range ex:
rand(3,5) (range def (0,1))
0.8147 0.9134 0.2785 0.9649 0.9572
0.9058 0.6324 0.5469 0.1576 0.4854
0.1270 0.0975 0.9575 0.9706 0.8003
~= operator
logical operators symbol
determine inequality, like a not function
&. |, ~(not)
what are M-files and what is a script file
.m at the end of their name
ASCII text files
two M-files: Script files: and function files
script files: A script file contains a set of valid commands, just like you would enter directly into the Command Window.
how to create a function file matlab
a group of statements that together perform a task.
functions are defined in separate files.
Functions can accept more than one input arguments and
may return more than one output arguments.
function_name =>same as file name
(without the m.extension)
function [sum,prod]= my name (a,b,c)
sum = a+b+c;
prod=abc
end
command window
» funcnam(20,21,22)
ans =63
>[s,p]=funcname(20,21,22)
s = 63, p =9240
%s,p are just vars can be anything, even a,b
for loop syntax
while loop syntax
all conditional statements
repeat a statement for a fixed number of times.
for m=1:3
num=1/(m+1)
end
OUTPUT:
num = 0.5000
num = 0.3333
num = 0.2500
while loop:
repeatedly executes statements while condition is true.
a = 10;
while(a < 15)
fprintf(‘value of a: %d\n’, a);
a = a + 1;
end
%d is a format specifier, d in this case is integer
\n is next line
all conditional statements
decides whether a particular block of code
has to be executed or not, based on Boolean
condition. condition is true, it executes
if-else
» number = 25
if number<20
fprintf(‘The number is greater than 20’)
else
fprintf(‘The number is not less than 20’)
end
if-elseif-elseif-else-end
number of end statements depend on the number of if statements
switch mat lab
is used to test for the equality against a set of known values
grade = ‘B’;
switch(grade) case 'A' fprintf('Excellent!\n'); case 'B' fprintf('Well done\n'); case 'C' fprintf('Well done\n'); case 'D' fprintf('You passed\n'); case 'F' fprintf('Better try again\n'); otherwise fprintf('Invalid grade\n'); end
plot y=x^2
The script file is as follows:
»_space; x = [-100:20:100]; 20
is increments, and the others are the range
» y = x.^2; [Here the “dot” is necessary]
»_space; plot(x, y)
- sinx:»_space; t = [0:0.1:2*pi]
» a = sin(t);
» plot(t,a)
3D graph examples
3D surface map for the function
𝑧=𝑥𝑒^(−(𝑥^2+𝑦^2 ) )
[x,y] = meshgrid(-2:.0.2:2);
z = x.* exp(-x.^2 - y.^2);
surf(x,y,z)
surf to create a surface plot
mesh grid points over the domain of the function(the range of value(horizontal and vertical lines on the 3D-shape)
another 3d graph examples
𝑧=sinx+cosy
[x,y] = mesh grid(-2:.2:2);
%default coordinates
»_space; z=sin(x)+cos(y);
»_space; surf(x,y,z)
surf to create a surface plot
mesh grid points over the domain of the function(the range of values)
xlabel and y label commands for x, y axis for graphs
> > x = [0:0.01:10];
»_space; y = sin(x);
»_space; plot(x, y), xlabel(‘x’), ylabel(‘Sin(x)’), title(‘Sin(x) Graph’)
polar coordinates
polarplot(theta,rho).
theta=>angle in radians rho=> radius value for each point/height of sin cos wave combo
> > theta = 0:0.01:2pi;
rho = sin(2theta).cos(2theta);
polarplot(theta,rho)
another example of polar coordinates
Plot the curve 𝑟=1−𝑠𝑖𝑛𝜃
> > theta = 0:0.01:2*pi;
rho = 1 - sin(theta);
polar(theta, rho)
polarplot(theta,rho).
theta=>angle in radians rho=> radius value for each point/height of sin cos wave combo
parametric curve
x=sin(t); y=cos(t); z=t
fplot3(xt,yt,zt), whichplots the parametric curve xt=x(t), yt=y(t), and zt=z(t) over the default interval−5<𝑡<5
default parameter range[-5 5].
> > syms t
xt = sin(t);
yt = cos(t);
zt = t;
fplot3(xt,yt,zt[-5,5])
parametric plots, with different range
Plot the parametric curve 𝑥=𝑒^(−𝑡/10)sin(5𝑡);
𝑦=𝑒^(−𝑡/10)cos(5𝑡);
z=t;
[-10 10]
> > syms t
xt = exp(-t/10).sin(5t);
yt = exp(t/10).cos(5t);
zt = t;
fplot3(xt,yt,zt,[-10 10])
Radius of curvature
Find the radius of curvature of the curve 𝑥^(2/3)+𝑦^(2/3)=𝑎^(2/3) at any point (x,y) of the curve.
syms = Creates symbolic variables: It allows you to define variables that can represent unknowns in mathematical expression
RADIUS FORMULA
d=(1+G(x,y)^2)^3/2))/c
c=a+b*G(x,y)
rho=d/c
> > syms x y a
F(x,y)=x^(2/3)+y^(2/3)-a^(2/3);
dy_dx = - diff(F,x)/diff(F,y)
G(x,y)=-y^(1/3)/x^(1/3);
a=diff(G,x);
b=diff(G,y);
c=a+bG(x,y)
simplify(c)
d=(1+G(x,y)^2)^(3/2))
rho=d/c
simplify(rho(x,y))
Out put: (y^(2/3)/x^(2/3) + 1)^(3/2)/(1/(3x^(2/3)y^(1/3)) + y^(1/3)/(3x^(4/3)))
Radius of curvature using theta
e^2*theta
theta formula
rho = (r^2+(r1^2))^3/2/ (r^2+2r1^2-r*r2^2)
syms theta
» r=exp(2theta);
» r1=diff(r,theta);
» r2=diff(diff(r,theta));
» a=(r^2+r1^2)^(3/2)
» b=r^2+2r1^2-rr2
» rho=a/b;
simplify(rho)
Out put: 5^(1/2)exp(4*theta)^(1/2)
why is a dot used in graph plotting and numerical integration
scaler: single value,
vector: 1D-array
MATLAB can only do multiplication on matrix, when ever there is a vector quantity u need to put a dot
Dots are used in MATLAB to specify element-wise operations when working with vectors or matrices.
.*, .^, ./
special case:
trigonometric functions like neither vectors nor matrices themselves. Instead, they are element-wise functions
and exp also
but exp(-x.^2 - y.^2);
inside u should still but dot for these
and sin(-x.^2 - y.^2);
partial derivative of a function, syntax for fxy
f=sin(𝑥)+𝑦^3+𝑥^10−𝑦^2+log(𝑥), then find 𝑓𝑥 & 𝑓𝑦. etc
double partial derivation
f=𝑥^2+2∗𝑦^2−22,then find 𝑓𝑥𝑥 & 𝑓𝑦𝑦
> > syms x y
f=sin(x)+y^3+x^10-y^2+log(x);
diff(f,x)
diff(f,y)
diff(f,x,y)
diff(f,y,x)
double partial derivation
»_space; syms x y
»_space; f=x^2+2*y^2-22
»_space; diff(f,x,2)
»_space; diff(f,y,2)
ans 2,4
Taylor and maclaurin
Expand f(x)=𝑒^𝑥𝑠𝑖𝑛𝑥 about the point x=2 up to third degree terms.
» syms x
» f = exp(xsin(x));
» t= taylor(f, ‘ExpansionPoint’,2, ‘Order’, 3)
Out put:
t=exp(2sin(2)) + exp(2sin(2))(2cos(2) + sin(2))(x - 2) + exp(2sin(2))(x - 2)^2(cos(2) - sin(2) + (2cos(2) + sin(2))*(cos(2) + sin(2)/2))
Taylors Theorem theorem without giving a value only degree
Expand f(x)=log(secx) about the origin up to six degree terms.
» syms x
» f = log(sec(x));
» T= taylor(f, ‘Order’, 7)
Out put:
T = x^6/45 + x^4/12 + x^2/2
taylors f(x,y)
Expand f(x,y)=e^x𝑐𝑜𝑠𝑦 about the point 𝑥=1,𝑦=𝜋/4 up to three degree terms.
> > syms x y
f=exp(x)cos(y);
t = taylor(f, [x, y], [1,pi/4], ‘Order’, 3)
Out put:
T = (2^(1/2)exp(1))/2 - (2^(1/2)exp(1)(y - pi/4)^2)/4 + (2^(1/2)exp(1)(x - 1)^2)/4 - (2^(1/2)exp(1)(y - pi/4))/2 + (2^(1/2)exp(1)(x - 1))/2 - (2^(1/2)exp(1)(y - pi/4)*(x - 1))/2
f(x,y)=0
Expand 𝑓(𝑥,𝑦)=𝑒^𝑦 log(1+𝑥) about the origin up to fourth degree
terms.
> > syms x y
f=exp(y)log(1+x);
T= taylor(f, [x, y], ‘Order’, 4)
Out put:
T= x^3/3 - (x^2y)/2 - x^2/2 + (xy^2)/2 + xy + x
why is syms used
and why is ode used
syms command is used to define symbolic variables. These are variables that represent mathematical symbols rather than numeric values. Symbolic variables allow you to perform symbolic computations, such as differentiation, integration, algebraic manipulation,
ODE in MATLAB refers to solving Ordinary Differential Equations numerically or symbolically. MATLAB offers various built-in functions and tools for handling ODEs.
differential equations and boundary condition
𝑑𝑦/𝑑𝑡=𝑡𝑦
Q2
Solve the differential equation: 𝑑𝑥/𝑑𝑡=𝑥+𝑡, given 𝑥(0)=0.
> > syms y(t)
ode = diff(y,t) == ty;
ySol(t) = dsolve(ode)
Output: ySol(t) =C1exp(t^2/2)
Q2
» syms x(t)
» ode = diff(x,t) == x+t;
» xSol(t) = dsolve(ode)
Output: xSol(t) =C1*exp(t) - t – 1.
To find the value of C1, we use:
» cond = x(0) == 0;
xSol(t) = dsolve(ode,cond)
Output: ySol(t) =exp(t) - t - 1
PDE arbitrary constants:
z=(x-a)^2+(y-b)^2
syms x y a b p q
z=(x-a)^2+(y-b)^2;
eq1=p==diff(z,x)
c1=solve(eq1,a)
eq2=q==diff(z,y)
c2=solve(eq2,b)
pde=subs(z,a,c1)
pde=subs(pde,b,c2)
arbitrary another example
2z=x^2/a^2+y^2/b^2
syms x y a b p q
z=x^2/(2a^2)+y^2/(2b^2)
eq1=p==diff(z,x)
c1=solve(eq1,a)
eq2=q==diff(z,y)
c2=solve(eq2,b)
pde=subs(z,a,c1)
pde=subs(pde,b,c2)
Plot the graph of solution of first order differential equations
𝑑𝑥/𝑑𝑡=𝑥+𝑡, given 𝑥(0)=0.
𝑑𝑥/𝑑𝑡=𝑥+𝑡, given 𝑥(0)=0.
tspan=[0 2]; % specify time span
x0=0; % specify x0 [t,x]=ode45(@(t,x)x+t,tspan,x0);
% now execute ode45
disp([t,x])
𝑡 is a vector with all discrete points ; and 𝑥 contains the values of the variable 𝑥.
plot(t,x) % plot t verses x
(𝑥^2−1) 𝑑𝑦/𝑑𝑥+2𝑥𝑦=1, given 𝑦(0)=1.
> > y0=1;
x0=0;
xend=[1,5,10]; same for all values,
This specifies the end points for x where the solution will be evaluated.
xspan=[x0,xend];
Combines the start (x0) and end points (xend) into an array, defining the intervals over which the ODE is solved.
[x,y]=ode45(@(x,y)(1-2xy)/(x^2-1),xspan,y0);
disp([x,y])
plot(x,y)
non-linear differential equation
(dy/dt+y)^2=1, y(0)=0
> > syms y(t)
ode = (diff(y,t)+y)^2 == 1;
ySol(t) = dsolve(ode)
Output: ySol(t) =
C1exp(-t) + 1; C2exp(-t) – 1
To find the values of C1 and C2, we use:
cond = y(0) == 0;
ySol(t) = dsolve(ode,cond)
Output: ySol(t) =exp(-t) – 1; 1 - exp(-t)
radius of curvature of the parametric curve
Formula of Parametric Curve
(x1^2+y1^2)^3/2/
(x1y2-y1x2)
=6𝑡^2−3𝑡^4,
𝑦=8𝑡^3.
» syms t
» x=6t^2-3t^4;
» x1=diff(x,t);
» x2=diff(diff(x,t));
» y=8t^3;
» y1=diff(y,t);
» y2=diff(diff(y,t));
» a=(x1^2+y1^2)
» b=simplify(a)
» c=(x1y2)-(y1x2)
» d=simplify(c)
» e=b^(3/2)
»rho=e/d
»simplify(rho)
Out put: rho= (6(t^2(t^2 + 1)^2)^(3/2))/(t^2(t^2 + 1))
Higher Order differencial
𝑦′′ + 9𝑦 = 0.
> > syms y(x)
ode = diff(y,x,2)+9y == 0;
ySol(x) = dsolve(ode)
Output:
ySol(x) =C1cos(3x) - C2sin(3*x)
solve y’‘-3y’+2y=0
y(0)=-1, y’(0)=0
> > syms y(x)
Dy = diff(y);
D2y = diff(y,2);
ode = D2y - 3Dy + 2y == 0;
ySol = dsolve(ode, y(0) == -1, Dy(0) == 0)
% Define The Initial»_space; Condition For ‘Dy(0)’ To Be ‘Some Value’
figure
ezplot(ySol)
Output: ySol = exp(2x) - 2exp(x)
Solve d^4y/dx^4+
8d^2y/dx^2+16y=0
> > syms y(x)
ode = diff(y,x,4)+8diff(y,x,2)+16y == 0;
ySol(x) = dsolve(ode)
Output:
ySol(x) =C1cos(2x) - C3sin(2x) + C2xcos(2x) - C4xsin(2x)
Solve: 𝑦′′′′ − 3𝑦′′′ + 2𝑦′′= 0, y(0) = 0, 𝑦′(0) = 0, 𝑦’′(0) = 2, 𝑦′′′(0) = 2
> > syms y(x)
Dy = diff(y);
D2y = diff(y,2);
D3y = diff(y,3);
D4y = diff(y,4);
ode = D4y - 3D3y + 2D2y == 0;
ySol = dsolve(ode, y(0) == 0, Dy(0) == 0,D2y(0) == 2,D3y(0) == 2)
figure
» ezplot(ySol)
Output:
»ySol =
2exp(x) - 2x - 2
graph also forms
Non homogeneous differential equation
d^2y/dx^2-4dy/dx +4y=2e^2x
> > syms y(x)
ode= diff(y,x,2)-4diff(y,x,1)+4y ==2exp(2x);
ySol(x) = dsolve(ode)
Output:
ySol(x) =x^2exp(2x) + C1exp(2x) + C2xexp(2*x)
Non homogeneous differential equation
Solve: 𝑦’′+4𝑦′ +13𝑦 = 18𝑒^−2𝑥 y(0) = 0,
𝑦′(0) = 4.
> > syms y(x)
Dy = diff(y);
D2y = diff(y,2);
ode = D2y + 4Dy + 13y == 18exp(-2x);
ySol = dsolve(ode, y(0) == -1, Dy(0) == 4)
figure
ezplot(ySol)
Output:
Sol =exp(-2x)(2sin(3x) - 9cos(3x) + 6))/3
and graph
Beta and gamma functions
Evaluate: Γ 0.5 , Γ 1 , Γ3/2
Γ7/2 Γ1/4 Γ3/4
- Evaluate several values of the gamma function between [-3.5 3.5].
gamma(0.5) ans = 1.7725
2. gamma(1) ans = 1
3. gamma(3/2) ans = 0.8862
4. gamma(7/2) ans = 3.3234
5.gamma(1/4)*gamma(3/4) ans = 4.4429
MATLAB CODE:
x = -3.5:3.5;
y = gamma(x)
MATLAB OUTPUT:
y = 1×8
0.2701 -0.9453 2.3633 -3.5449 1.7725 0.8862 1.3293 3.3234
Graph of gamma function
3. Use fplot to plot the gamma function and its reciprocal.
(Or) Geometrical representation of Gamma function: Γ 𝑛 =
0 ->∞ ∫𝑒^−𝑥*𝑥^(𝑛−1)𝑑𝑥
𝑛 > 0
fplot(@gamma)
hold on
%hold on: retains the current plot, additional plots to be added without erasing the existing one.
fplot(@(x) 1./gamma(x))
hold off
%releases the current plot, preventing further plots from being added to the existing axes.
legend(‘\Gamma(x)’,’1/\Gamma(x)’)
grid on
fplot(@gamma)
hold on
fplot(@(x) 1./gamma(x))
ylim([-10 10])
legend(‘\Gamma(x)’,’1/\Gamma(x)’)
hold off
grid on
Evaluation of Beta function
- Compute the beta function for integer arguments n=3 and m=1,…,10.
- beta(2,3) ans = 0.0833
- beta(2,5) ans = 0.0333
- beta(0,1) ans = Inf
4) MATLAB CODE:
format rat
B = beta((1:10)’,3)
‘ => for nums to be vertical/next line
OUTPUT:
B= 10*1…
Subs what is it
used for symbolic substitution in symbolic math expressions. replaces variables or parts of an expression with specific values, other variables, or even expressions.
subs with values or other vars