You are on page 1of 28

APPLICATION OF DIFFERENTIAL EQUATIONS

FALLING BODIES
Q1:

Rewrite the second order function in terms of y1, y2 see your


notes on that

Let Y1=x
Y2=y1=dx_dt
Y2= d2x_dt2
Then you will need to create 2 m-files a follow
Diffential Equation Codes
function dh_dt=diff_fun(t,y)
global c g m
dh_dt=[y(2);c*x(2)/m-g];

end

Main codes
function diff_main
global m c g
m=50;
g=9.8;
c=0.1;
options=odeset(RelTol,1e-6);
tspan=[0,10];
y0=[1000;0];
[t,x]=ode45(@diff_fun,tspan,y0,options)
height= x(end,1) %this represents height at 10sec
%calculate change in height
dh= gradient (x(:,1));
h_10= dh(end);
%calculate change in time
dt= diff(t);
t_10=dt(end);
%calculate change in velocity
velocity = h_10/t_10
%plot figure to see motion
plot(t,x(:,1),r-);
legend(h);
ylabel(height [m]);
xlabel(time [s])
end
To edit the line weight and font weight of your plot
Plot (t,x(:,1),r-,linewidth,2)
Xlabel(time[s],fontweight,bold)

Q2 is similar to q1 and see notes on that

One extra global factor added d

Q3

Diffential Equation Codes


function dx_dt=diff_fun(t,x)
global c m k
dx_dt=[x(2);
(-c*x(2)-k*x(2)+20*cos(2*t))/m];
end
Main codes
function diff_main
global m c k
options=odeset("RelTol",1e-6);
x0=[1;4];
m=1;k=2;c=3;
tspan=[0,5];
[t,x]=ode45(@diff_fun,tspan,x0,options)
figure;

hold on;
plot(t,x(:,1),"r");
legend("h");
ylabel("h");xlabel("t")
Q4

Diffential Equation Codes


function dx_dt=diff_fun(t,h)
global R g
dx_dt=-R^2*sqrt(2*g*h(1));
end

Main codes
function diff_main
global R g
options=odeset("RelTol",1e-6);
x0=[10];
R=0.1;g=9.8;
tspan=[0,200];
[t,x]=ode45(@diff_fun,tspan,x0,options)
figure;
hold on;
plot(t,x(:,1),"r");
legend("h");
ylabel("h");xlabel("t")
Q5

Let

, then

function dx_dt=ode_fun3(t,x)
dx_dt=[45*cos(pi/3);
x(3);
-9.8];
end
options=odeset("RelTol",1e-6);
x0=[0;1;645sin(pi/3)];
tspan=[0,15];
[x,y]=ode45(@ode_fun3,tspan,x0,options);
figure;
hold on;
plot(y(:,1),y(:,2),"g");
legend("x","y");
ylabel("y");xlabel("x");

Working with Data (see notes)


Q1: Down load the "data1.dat" From Lecture 6 in the
presentation folder. Two sets of data (x and y) are included.
(1) Find a polynomal experssion with a degree of 6 to fit the
orginal data?
(2) Using the obtained polynomal model, calculate the following
value.

y(0.5)=?
m-file:

load data1.mat
p_coeffs = polyfit(x,y,6);
figure
plot(x,y,"o-")
hold on
yfit = polyval(p_coeffs,x);
plot(x,yfit,"r-","LineWidth",2);
legend("Data","Polynomial Fit");
polyval(p_coeffs,0.5)
Q2

WRITE M-FILE:

F=[15;15;15;15;30;30;30;30];
V=[1091.3; 1838.7;1095.7; 1820.3; 1031.7; 1642.3; 1033.7;1631];
Theta=[54.3;56;180.7;180;64.7;76.7; 200.3; 200.7];
T=[20; 11.7; 16.3; 11.3; 34.3; 29.3; 32.7; 28.3];
X=[ones(size(F)) log(F) log(V) log(Theta)];
y=log(T);
ce=X\y;
a=exp(ce(1))
b=ce(2)
c=ce(3)
d=ce(4)
F1=24.7;
V1=1350;
T1=155;
T1=a*(F1^b)*(V1^c)*(T1^d)
Answers found:
a=110.2811
b= 1.0498
c= -0.6269
d= -0.0718
T(24.7,1350,155)=30.5184

Q3

D=[0.3048; 0.6096; 0.9144; 0.3048; 0.6096; 0.9144; 0.3048;


0.6096;0.9144];
S=[0.001;0.001;0.001;0.01;0.01;0.01;0.02;0.02;0.02];
Q=[0.3964; 2.3503; 6.8527;
1.3309;8.1836;23.7863;3.1432;19.5387;56.634];
X=[ones(size(D)) log(D) log(S)];
y=log(Q);
ce=X\y;
a=exp(ce(1))
b=ce(2)
c=ce(3)

D1=1;
S1=0.1;
T1=a*(D1^b)*(S1^c)
And answers found:
a=785.7008
b=2.6519
c=0.6596
Q(1,0.1)=172.0434
Q4 working with excel file

Xlsread(stress_strain)
%test1 data
X1= Data(:,1);
Y1=Data(:,2);

%test2
x2= data(:,3);
y2=data(:,4)
and so on *see notes on this topic and matlab code*
answers found:

y -- stress
x -- strain
Test 1:
y=-17.9040*x6+113.9561x5 -278.0313x4+328.2540x3 -185.8232x2
+47.6729x +2.4942

Differential Equations

function dx_dt=diff_fun(t,x)
dx_dt=x-1;
end
%main codes
options=odeset("RelTol",1e-6);

x0=[0.25];
tspan=[0,0.4];
[t,x]=ode45(@diff_fun,tspan,x0,options);
figure
hold on
plot(t,x(:,1),"r");
plot(t,x(:,2),"g");
legend("x1","x2");
ylabel("x");
xlabel("t")
Q2

function dx_dt=diff_fun(t,x)
dx_dt=[x(1)+2*x(2)-x(3);x(1)+x(3);
4*x(1)-4*x(2)-5*x(3)];
end
%main code
options=odeset('RelTol',1e-6);
x0=[0,1,1];
tspan=[0,1];
[t,x]=ode45(@diff_fun,tspan,x0,options);
figure
hold on

plot(t,x(:,1),'r');
plot(t,x(:,2),'g');
plot(t,x(:,3),'b');
legend('x','y','z');
ylabel('x');
xlabel('t')

Q3

function dy_dx=diff_fun(x,y)
dy_dx=x*y*y-y;
end
%main code
options=odeset("RelTol",1e-6);
x0=[1];
tspan=[0,5];
[t,x]=ode45(@diff_fun,tspan,x0,options);
figure
hold on
plot(t,x(:,1),"r");
ylabel("y");
xlabel("x")

Q4

function dy_dx=diff_fun(t,x)
dy_dx=[x(1)-x(2);-x(2);x(1)*sin(x(3))];
end
%main code
options=odeset("RelTol",1e-6);
x0=[1,1,1];
tspan=[0,1];
[t,x]=ode45(@diff_fun,tspan,x0,options);
figure
hold on
plot(t,x(:,1),"r");
plot(t,x(:,2),"g");
plot(t,x(:,3),"b");
legend("x","y","z");
ylabel("y");
xlabel("x")
Q5

function dy_dt=diff_fun(t,y)
dy_dt=[y(2);-3*y(2)-y(1)+4*exp(-2*t)-5];
end
%main code
options=odeset("RelTol",1e-6);

x0=[2,-1];
tspan=[0,5];
[t,x]=ode45(@diff_fun,tspan,x0,options);
figure
hold on
plot(t,x(:,1),"r");
plot(t,x(:,2),"g");
%plot(t,x(:,3),"b");
legend("x","y","z");
ylabel("y");
xlabel("x")
Q6

Integrals

MAKE SURE YOU DO NOT WRITE >> IN YOUR ANSWER


CODE, YOU WILL HAVE NO MARKS BECAUSE THE
SOFTWARE DOES NOT UNDERSTAND THIS.
ITS BETTER TO WRITE AN M-FILE RATHER THAN HAVING IT
IN THE COMMAND LINE, FOR YOUR TEST
Q1 >>X = 0:pi/100:pi;
>>Y = sin(X);
>>Z = trapz(X,Y)
or
>>Z = pi/100*trapz(Y)
Q2
h=1;n=100;
X = 0:h/n:h;
Y=sin*(X).*exp(2*X);
Z = trapz(X,Y)

Q3
>> f=@(x)(x.^3+sin(3*x));
>> quad(f,1,2)
Q4
>> f=@(x)(exp(2*x).*sin(x.^4-2*x))
>> quad(f,-5,5)

Derivatives
START BY CREATING AN M-FILE, THE
ONE FROM Q4

Q1 AND 2 WORK THE SAME WAY


Method 1
>>y =[0.9950,0.9801,0.9553]
>>g=diff(y)/0.1
>>s=diff(g)/0.1

or
Method 2
>>y =[0.9950,0.9801,0.9553]
>>g=gradient(y,0.1);
>>s=gradient(g,0.1);
Q3

>>y =[1.5017,1.64095,1.7845,1.9069,2.0176,2.1179]
>>g=gradient(y,0.1)
>>s=gradient(g,0.1)
Q4

Create an m-file like this one:


function[df]=mygradient(f,x0,h)
s1=size(x0);
y1=f(x0);
df=x0;

if(h<0)
h=0.001;
end
fori=1:s1(1)
forj=1:s1(2)
t=x0(i,j);
x0(i,j)=t+h;
y2=f(x0);
df(i,j)=(y2y1)/h;
x0(i,j)=th;
end
end
end
then go on command line and define your function
f= @(x)(sin(x)*exp(x))
then type
my gradient(f,0.1,0.001)
my gradient is forvectors and diff is for matrix

Simultaneous Equations

Q1: BRING EVERYTHING TO ONE SIDE AND WRITE AN MFILE


function F=myse1(x)
F(1)=2*x(1)-x(2)-exp(-x(1));
F(2)=-x(1)+2*x(2)-exp(-x(2));
end
x0=[-5;-5];
options=optimset("Display","iter");
[x,fval]=fsolve(@myse1,x0,options);
x
Q2: REQUIRES ONLY SOLVING WITH NO INITIAL VALUES OF
X SO CAN SOLVE DIRECTLY ONTO COMMAND LINE. BUT
FOR THE TEST IT IS RECOMMENDED TO WRITE AN M-FILE
FOR YOUR ANSWER!!!!!!!!!
A=[0.5,1.1,3.1;2,4.5,0.36;5,0.96,6.5]
b=[6;0.02;0.96]
x=A\b
Q3:
a=[4 1 2;3 8 -1;2 -1 -4]

b=[4;20;4]
x=a\b

Q4
IT CAN BE SOLVED LIKE Q3
Q5
IT HAS INITIAL VALUES AND NEEDS TO BE SOLVED LIKE Q1
function F=myse1(x)
F(1)=2*x(1)-x(2)-3*x(3)-exp(-x(1));
F(2)=-x(1)+3*x(2)+x(3)-exp(-x(2));
F(3)=-2*x(1)-x(2)+2*x(3)-exp(-x(3));
end
>>x0=[-5;0;1];
>>options=optimset("Display","iter");
>>[x,fval]=fsolve(@myse1,x0,options);
>>x

Roots of Functions

Q1
>>f=@(x)(x^2*exp(-x)-1);
>>x=fzero(f,1)
Q2
>>f=@(x)(sin(x)*exp(x)-1);
>>x=fzero(f,1)
Q3
roots([1 0 0 2 -5])

Q4
>> f=@(x)(2*tanh(0.2*x-3)-0.2*x+2.8);
>> [x,y]=fzero(f,5)
Q5 CAN BE SOLVED LIKE Q3

Errors

q1: Large + small, difference of two numbers close together AND


division by small number, large + small.
And the matlab code for it is:
function [ result ] = exp1(t,eps)
result=1;
n=1;
item=1;
while(abs(item)>eps)
item=(t^n)/factorial(n);
result=result+item;
str = sprintf(Item %d = %f (%f),n+1, item, result);
disp(str);
n=n+1;
end
for the function can also write

function [ result ] = exp1(t, erro)


Q2
Expressions evaluate to 0 and 0.0625 (exact) in 3 sig. fig.
arithmetic
THE MATLAB CODE
function [ result ] = sin1(t,eps)
result=1;
n=1;
item=eps+1;
while(abs(item)>eps)
nn=2*n-1;
item=((-1)^(n+1))*(t^nn)/factorial(nn);
result=result+item;
str = sprintf("Item %d = %f (%f)",n, item, result);
disp(str);
n=n+1;
end
Introduction to Matlab

Q1 >>f=@(x)(x^2+2*x+1);
or write a M-File
function y=f(x)
y=x^2+2*x+1;
Q2
>> a=[1,2,3;4,5,6;7,8,9]
>>inv(a)
Q3
>>factorial(10);
Q4
>> (5^5/3)*11
Q5
>>f=@(x,y)(exp(2*x+1)*sin(y));
or write a M-File

function z=f(x,y)
z=exp(2*x+1)*sin(y);
Q6

You might also like