You are on page 1of 3

Introduction

Function writing

function out= trial3(s,omega,t)


out = exp(-s.*t).*sin(omega.*t);
plot(t,out)
end

Anonymous functions

f= @(x) x + cos(2*x);
f(1)

f(1:7);
ezplot(f)
fplot(f)

Analytical method)

clear
clc
syms y(x)
eqn=diff(y)== 3*x^2+3;
cond=y(0)==0;
ysol=dsolve(eqn,cond);
fplot(ysol)
1. Euler’s normal method

function yout= euler45(F,t0,h,tfinal,y0)


y=y0;
yout=y;
for t=t0:h:tfinal-h
s=F(t,y);
y=y+h*s;
yout=[yout;y];
end
end

% Refer function file euler 45


clear all
clc
F=@(t,y) 0.2*t*y;
t0=1;
h=0.1;
tfinal=1.5;
y0=1;
yout= euler45(F,t0,h,tfinal,y0);
t=t0:h:tfinal;
y_exact= exp(0.1.*((t.^2)-1));
plot(t,yout,'r',t,y_exact,'k'),

hold on

___________________________________________________
2. Example ODE 45;

f=@(x,y) (x-y)^2;
[x,y]= ode45(f, 0:0.01:0.5,0.5);

3. 2nd Order using ODE 45:

Mass spring damper:

f= @(t,x) [x(2); (1/5)*(sin(t)-4*x(1)-7*x(2))];

[t1,x1]= ode45(f, (0:0.1:12), [3, 9]);

plot(t,x(:,1),'r',t,x(:,2),'b'),legend('displacemen
t','velocity')

4. Two mass model using ODE 45:

f =@(t,x) [x(3);x(4);0.2*(-12*x(3)-5*x(1)+8*x(4)+4*x(2));0.33*(-
8*x(4)-4*x(2)+8*x(3)+4*x(1))];

[t,x]= ode45(f,0:0.1:7,[5,1,-3,2]);

plot(t,x), legend('dispm1','dispm2','velm1','velm2')

You might also like