Professional Documents
Culture Documents
Date: 04/01/2018
Mon : 16:00-17:25; Thurs: 14:30-15:55; Room No. C-LH4
a) Solution of Algebraic equation: Incremental search method, Bisection method, Ridder’s method, Secant
method, Newton-Raphson methods.
c) Solution of IVP and BVP: Euler, RK –different order, Shooting method, Finite difference method
References:
Grading: 1)Ramin S. Esfandian, “Numerical methods for Engineers and Scientist” CRC Press,,
1) Attendence: 10% New York 2013.
2) Assignments+Quiz (3): 30% 2) Rudra Pratap, “Getting started with MATLAB: A Quick Introduction to Scientists and
3) Mid sem (4 Feb): 30% Engineers”Oxford University Press, Latest Edition.
4) End sem (14 March): 35% 3)Jaan Kiusalaas, “Numerical methods for Engineering with Matlab” Cambridge
University Press, Cambridge, 2010.
4)Firdaus E. Udwadia abd Robert E. Kalaba, “Analytical Dynamics: A new approach”
Total 100%
iv) Open a new Editor and name it accordingly, for example “Example_1.m”
>> edit Example_1.m
(A prompt opens! To create a blank file with name Example_1.m, click Yes)
v) First Command in the Edit file should be
>> clear all; (If you are not using any old information.)
>> clc; (To clear the screen!)
v) To display any statement
>> display('Welcome to Hyderabad') % Automatically goes to next command
>> input('Welcome to Hyderabad') % Press any button to continue…
v) For any help, type “help” command in command prompt, for exp. help regarding display
>> help 'display’
>> help display
Some Basic Operators
Arithmetic operators.
plus - Plus +
uplus - Unary plus +
minus - Minus -
uminus - Unary minus -
mtimes - Matrix multiply *
times - Array multiply .*
mpower - Matrix power ^
power - Array power .^
mldivide - Backslash or left matrix divide \
mrdivide - Slash or right matrix divide /
ldivide - Left array divide .\
rdivide - Right array divide ./
idivide - Integer division with rounding option .
kron - Kronecker tensor product
Some More Operators
Relational operators. Logical operators.
eq - Equal ==
ne - Not equal ~= relop - Short-circuit logical AND &&
lt - Less than < relop - Short-circuit logical OR ||
gt - Greater than > and - Element-wise logical AND &
le - Less than or equal <= or - Element-wise logical OR |
ge - Greater than or equal >= not - Logical NOT ~
Relational operators.
colon - Colon : punct - Continuation ...
paren - Parentheses and subscripting () punct - Separator ,
paren - Brackets [] punct - Semicolon ;
paren - Braces and subscripting {} punct - Comment %
punct - Function handle creation @ punct - Invoke operating system command !
punct - Decimal point . punct - Assignment =
punct - Structure field access . punct - Quote '
punct - Parent directory .. transpose - Transpose .'
ctranspose - Complex conjugate transpose '
Numbers, Functions, and Equations
Numbers: Scalar, Vector, and Matrix
>> x=linspace(1,10,5)
Answer: [1.0000 3.2500 5.5000 7.7500 10.0000]
>> x=[1:2.25:10];
Answer: [1.0000 3.2500 5.5000 7.7500 10.0000]
I. Inbuilt functions, f(x): Most commonly used functions are already present in MATLAB.
Examples: i) Polynomial of different orders and all trigonometric functions and their inverse
such as sin, cos, tan, etc
ii) Exponential and logarithmic functions
iii) Bessel’s function of different order
iv) Many more standard/special functions.
II. Other functions: Functions different from inbuilt functions can be obtained by performing
different operations such as addition, subtraction, product and division with
different inbuilt functions and polynomial functions using predefined vector ‘x’.
Numbers, Functions, and Equations
Examples:
1. Linear equations
Linear Equations: ax=b, when, x is unknown, and a and b are constant terms
A linear equation of two variable is a straight line in 2D.
A linear equation of three variable represents a plane in 3D.
A linear equation of n-variables represents a hyperplane in n-D.
3. Systems of equations
If f1(x)=0; f2(x)=0; f3(x)=0; f4(x)=0; f5(x)=0; …… , fN(x)=0; are N equations,
System of equations can be represented by a vector F(x)=[f1(x); f2(x); f3(x); ……, fN(x)]=0.
Creating a function file in MATLAB
Any function/equation or a system of equations can be written in MATLAB
The first line of a function m-file may be written in the following form
0.8
plot(Y)
plot(X1,Y1,…,Xn,Yn) 0.6
0.2
y=sin(x)
Plot y=sin(x) 0
>>x=-pi:0.1:pi; -0.2
>>y=sin(x); -0.4
Other related commands: axes, grid, subplot, xlim, ylim, zlim, axis
Plots-3D
Any function/expression can be plotted for a given range of inputs
Matlab Syntex:
40
plot(X1,Y1,Z1)
30
20
>>t=0:pi/50:10*pi; 0
1
>>x=sin(t);y=cos(t); z=t;
0.5 1
>> plot3(x,y,z) 0
0
>> grid on; -0.5
-1 -1
>> axis tight;
Other related commands: axis, bar3, grid, line, LineSpec, loglog, plot, semilogx, semilogy, subplot surf
Visualization
To visualize the variation of a function, different 2D plots, contour,
35
surface plots can be obtained
30
25
Visualization 20
15
>> view([-1, -1, 0.5])
10
5
5
0
1 1
0 0
0 -1 -1
3D Contour plots
>>[X,Y,Z] = meshgrid(-3:0.125:3) -5
>>Z=peaks(30);
>> meshc(XYZ) -10
>>surf(X,Y,Z)
2
3
>>surfc(XYZ) 2
0 1
>>colormap hsv -1
0
-2 -2
>>axis([-3 3 -3 3 -10 5]): -3
Other related commands:
axis, caxis, colormap, contour, delaunay, imagesc, mesh, meshgrid, pcolor, shading, trisurf, view
Breakout Sessions
Topic 1: Introduction to Numbers, Functions, Equation and systems of
equations, Matrix and their operations, Functions , Plots – 2D, 3D,
Visualization.
where,
Jacobian =
In Matrix form
Description
1. [x,fval,exitflag] = fzero(fun,x0,options); options = optimset('Display','iter','TolFun',1e-8)
2. [x,fval,exitflag,output,jacobian] = fzero(fun,x0,options);
options = optimset('Display','iter','TolFun',1e-8, 'Jacobian','on')
3. r=roots(c), where c is coefficients of polynomial ordered in descending order.
Example 4: r=roots(c).
>>g=inline('(x^2-4*x)*(x^2-4*x+1)-20');
>>sol=solve(g); 𝑝 𝑥 = 𝑐1 𝑥 𝑛 + 𝑐2 𝑥 𝑛−1 + ⋯ + 𝑐𝑛 𝑥 + 𝑐𝑛+1 = 0,
𝑐 = 𝑐1 , 𝑐2 , . . , 𝑐𝑛 , 𝑐𝑛+1 ;
Example 5
syms a b x y real Example 6: Solve 𝑥 3 − 6𝑥 2 − 72𝑥 − 27
f1='a*x+b*y-3'
f2='-x+2*a*y-5' >> c=[1 -6 -72 -27];
[x,y]=solve(f1,f2) >> r=roots(c);
Examples
[x,fval,exitflag,output,jacobian] = fzero(fun,x0,options);
[x,fval,exitflag] = fzero(fun,x0,options);
options = optimset('Display','iter','TolFun',1e-8, …
options = optimset('Display','iter','TolFun',1e-8)
'Jacobian','on'))
Example 1: Solution near 3
>>x = fzero(@sin,3); Example: Solve equations at x0=[-5.-5]
2𝑥1 − 𝑥2 = 𝑒 −𝑥1
Example 2: Solution between 1&2: −𝑥1 + 2𝑥2 = 𝑒 −𝑥2
>>x = fzero(@cos,[1 2])
Solution: Rewrite equation in the form F(X)=0
Example 3: Solution of 2𝑥1 − 𝑥2 − 𝑒 −𝑥1 = 0
>>f = @(x)x.^3-2*x-5; −𝑥1 + 2𝑥2 − 𝑒 −𝑥2 = 0
>>z = fzero(f,2) %near 2 %M-file function
function F = myfun(x,a)
Example 4: Solution of function F = [2*x(1) - x(2) - exp(-x(1));
%M-file function -x(1) + 2*x(2) - exp(-x(2))]
function f = myfun(x,a)
f = cos(a*x); %Script
%Script >>x0 = [-5; -5];
>>a=2; options=optimset('Display','iter');
>>x = fzero(@(x) myfun(x,a),0.1) [x,fval] = fsolve(@myfun,x0,options)
Exercise
Q1. Solve the following nonlinear problem (algebraic equations) simultaneously.
3X3+X2-1=0
X4-10X2+2=0
a) Write a function file for the above equations, solve individually and also
simultaneously those two equations.
b) Plot both these functions and find out approximate solution by looking at the
intersection points (to find initial guess).
c) Using initial guess solve for the exact solutions using ‘fsolve’ or ‘fzero’.
d) Also solve these equations using ‘solve’ command.
a) Using inline command define the function and use ‘fplot’ to plot the function
over an approximate interval of x and locate the zeros of the graph
b) Use fzero to find the accurate value for the initial values obtained in a)
c) Write coefficient vector and find solution using roots
d) Use symbolic f(x) to solve the roots of f(x) using solve command
Breakout Sessions
Topic 1: Introduction to Numbers, Functions, Equation and systems of
equations, Matrix and their operations, Functions , Plots – 2D, 3D,
Visualization.
Example 3: Specifying the independent Variable Example 6: Solving a System of Differential Equations
>>y = dsolve('(Dy)^2 + y^2 = 1','s') >> z = dsolve('Dx = y', 'Dy = -x')
>> y=dsolve('D2y = -a^2*y', 'y(0) = 1', 'Dy(pi/a) = 0‘)
Example 4: Specifying the independent Variable >>z.x
>>y = dsolve('(Dy)^2 + y^2 = 1', 'y(0) = 0') >>z.y
>>z
General Form of ODE
MATLAB Syntax:
1. dsolve: Symbolic solver for ODE
Solver:
ode23, ode45, ode113, ode15s, ode23s, ode23t, ode23tb:
Syntax:
[T,Y] = solver(odefun,tspan,y0)
[T,Y] = solver(odefun,tspan,y0,options)
options = odeset('RelTol',a,'AbsTol',b);
Syntax:
'F' - is the name of the file with the state space differential equations.
TSPAN - Tspan = [T0, Tfinal], initial and final integrating times. If we look for a solution at
specific times (T0, T1, T2, …, Tfinal), define Tspan = [T0 T1 T2, … Tfinal], and Ti = t0:t:tf.
OPTIONS - contains the optional values for the desired accuracy like:
Relative error tolerance 'RelTol' (le-3 by default) and
Vector of absolute error tolerances 'AbsTol' (all components le-6 by default).
2. Type "solve" from the MATLAB window and the file "system1" will be used automatically. The
results are stored in vector t and matrix x.
3.Plot solution graphs. The corresponding MATLAB command is "plot(x,y)" where x is an argument
column-vector (horizontal-axis), while y is a function column-vector (vertical axis). File "solve1.m"
includes all the necessary commands, which shows how to plot the graphs.
Time Response
%File "system1.m" - Dynamical System %File “solve1.m" - Dynamical System
figure(1)
subplot (2, 1, 1)
plot (t,x(:,1)); grid;
xlabel ('t [sec]'); ylabel ('x(1) [m]');
subplot (2, 1, 2)
plot (t,x(:,2)); grid;
xlabel ('t [sec]'); ylabel ('x(2) [m]');
Time Response and Phase Plane
Forced vibration: γ = 0.2, α = −1,β =1,δ = 0.3, ω =1.2
a) Time series
figure(2)
subplot (2, 1, 1)
plot (t,x(:,1)); grid;
xlabel ('t [sec]'); ylabel ('x(1) [m]');
% the steady state space is defined by the last 20
%percent of the solution vector.
% n is the total number of time nodes during
%integration a) Phase plane at steady state (120 sec<t<150 sec)
n=size(t);
subplot (2, 1, 2)
plot (x(round(0.8*n):n,1), x(round(0.8*n):n,2));
grid;
xlabel('x(1)[m]'); ylabel('x(2) [m/s]');
Poincare Map
• Poincare' Maps are used to characterize the stability of periodic system response
(e.g. whether the response is periodic with the same period as that of the excitation).
• A system with periodic response has a finite number of Poincare’ points in state space:
i) a single point: periodic response with the same period of excitation (T).
ii) m points: subharmonic response (of order m) corresponding to periodicity with an
integer multiple of the exciting period (e.g. mT, m>1).
iii) m points: ultrasubharmonic response (of order m/n) corresponding to periodicity
with an integer submultiple of the exciting period (e.g. mT/n, m,n>1).
• A system which exhibits aperiodic response has an infinite number of Poincare’ points
in its state space:
i) an infinite number of points organized along an invariant curve (or topology)
quasiperiodic response describing a beating like phenomena.
ii) an infinite number of points that are not! organized along an invariant curve describe
nonstationary response (and are a candidate for chaotic vibrations in deterministic
systems).
Poincare Map
Solve the system of two first order equations (equation 2), with
γ = 0.2, 0.28, 0.51, 0.46 , α = −1,β =1,δ = 0.3, ω =1.2
figure (1);
function dy=rigid(t,y,w)
global w w0 gamma1 beta1 q
dy=zeros(2,1);
dy(1)=-beta1*w0*y(1)+q/(2*w0)*sin(y(2));
dy(2)=(w-w0)-3*gamma1/(8*w0)*y(1)^2+q/(2*w0*y(1))*cos(y(2));
end
MATLAB Programing
Frequency Sweeping from 0.1 to 2.5
clc
clear all for i=1:1:length(ww)
w=ww(i)
global w w0 gamma1 beta1 q [T,Y] = ode45(@rigid,[0 200],Y0);
nn=length(Y(:,1));
gamma1=0.5; ymax=max(Y(nn-round(nn/4):nn,1));
beta1=0.05; q=0.2; ymax1=max(Y(nn-round(nn/4):nn,2));
w0=1; Y0=[ymax ymax1];
np=400; yy=[yy;ymax];
end
ww=linspace(.1,2.5,np);
ww1=linspace(2.5,.1,np); plot(ww,yy,‘b')
yy=[]; hold on
yy1=[];
Y0=[0.1 0.1];
MATLAB Programing
Frequency Sweeping from 2.5 to 0.1
ymax1=max(Y(nn-round(nn/4):nn,1));
ymax2=max(Y(nn-round(nn/4):nn,2));
for j=1:1:length(ww1)
w=ww1(j)
[T,Y1] = ode45(@rigid,[0 1000],[ymax1,ymax2]);
nn1=length(Y1(:,1));
ymax1=max(Y1(nn1-round(nn1/4):nn1,1));
ymax2=max(Y1(nn1-round(nn1/4):nn1,2));
yy1=[yy1;ymax1];
end
hold on
subplot(3,1,3)
plot(ww1,yy1, ‘r')
Thank you
References:
1. Rudra Pratap, “Introduction to MATLAB”
2. http://www.mathworks.in/help/matlab/math/partial-differential-
equations.html
3. http://www.math.tamu.edu/~efendiev/math647_spring05/pdemat.pdf
4. http://www.youtube.com/watch?v=fYSEY4Q10nk