Sie sind auf Seite 1von 15

CME 362 Linear

Programming
Assignment
Aaron Steinhof
Instructor: Professor H. Montazeri

CME362 Linear Programming Assignment


Aaron Steinhof
Part A) First order, inhomogeneous, linear ODE:
x ( t ) +x ( t ) = sin ct
=6
=50
c=9

x ( t )+6 x ( t ) =50 sin 9 t ; x ( 0 )=2


Homogenous Solution
x h ( t ) +6 x h ( t )=0

Particular Solution
x p ( t )=k 1 sin 9 t+ k 2 cos 9 t

d xh
=6 x h ( t )
dt

xp ( t ) +6 x p ( t ) =

1
d x =6 x h ( t ) dt
x h (t ) h

50 sin 6t
9 k 1 cos 9 t9 k 2 sin 9 t+6 k 1 sin 9 t +6 k 2 cos 9 t=50 sin 6 t

1
d x =6 ( t ) dt
xh ( t ) h

ln xh ( t )=6 t+c
6 t

x h ( t )=c e

( 6 k 19 k 2 ) sin 9t + ( 9 k 1+ 6 k 2 ) cos 9 t =50 sin 6 t

x ( t )=x h ( t ) + x p (t )

x ( 0 )=c e6 (0) +
c
x ( t )=

100
6 k 19 k 2=50
39
50
9 k 1 +6 k 2=0
k 2=
13

x p (t)=

x ( t ) =c e6 t +

d
( k sin 9t +k 2 cos 9 t ) +6 ( k 1 sin 9 t+ k 2 cos 9 t )
dt 1

100
50
sin 9t cos 9 t
39
13

100
50
sin 9(0) cos 9(0)=2
39
13

50
76
=2 c=
13
13

76 6 t 100
50
e +
sin 9 t cos 9 t
13
39
13

k 1=

100
50
sin 9 t cos 9 t
39
13

x ( 5) =

76 6 (5) 100
50
e +
sin 9 ( 5 ) cos 9 ( 5 )
13
39
13

76 30 100
50
e +
sin 45 cos 45
13
39
13

0.1613347213018903451150580163171508248958632701187105 [ . solution]

Part B)

o See Appendix A1 for MATLAB function for

x ( t )=50 sin 9 t6 x ( t )

o See Appendix A2 for MATLAB function for exact solution


76
100
50
x ( t )= e6 t +
sin 9 t cos 9 t
13
39
13

To arrive at a global error that was less than 5%, a n value and subsequent
t value was established, through trial and error. These values are as follows:
n=8724

t=

50
4
=5.7313159110
8724

Global Error=4.999861

The Eulers Method function, with n=8724 was plotted against the exact solution and is
displayed as follows
.

Image 1: Solution using Eulers Method with


n=8724

o See Appendix A3 for the MATLAB script.

To arrive at a global error that was less than 0.01%, following the same steps, the
t values are as follows:
subsequent n

n=436000

t=

50
5
=1.14678899110
436000

Global Error=0.0999
The Eulers Method function, with n=436000 was plotted against the exact solution and
is displayed as follows.

n=436000

See Appendix A4 for the MATLAB code.

Image 2: Solution using Eulers Method with

Part C) Equation 2.21 from Bill Goodwines Engineering Diferential Equations:


tn
t t
t
t
x ( t )=e e g ( t ) dt + x 0 e e
0

t0

g ( t )=50 sin 9 t
x 0=x ( 0 )=2
=6
5
6t

x ( t ) =e

e6 t (50 sin 9t )dt +2 e 6(0) e6 t


0

5
6 t

e6 t ( 50 sin 9 t ) dt +2 e6 t
0

Exact Solution:
x ( t )=2 e6 t +

50 6 t
e ( 3+e 30 ( 2 sin 453 cos 45 ) )
39

Evaluating the function at

x ( 5 ) =2 e6 (5 )+

x (5) :

50 6 (5)
e
( 3+e 30 ( 2 sin 453 cos 45 ) ) =0.1613
39

This is found to agree with my original, exact solution, to 4 decimal


places.

o See Appendix A5 for MATLAB function for exact solution


50 6 t
30
e ( 3+e ( 2 sin 453 cos 45 ) )
39
o See Appendix A6 for MATLAB function for Trapezoid Rule
6 t

x ( t )=2 e

o See Appendix A7 for MATLAB function for the integrand,


, which is used in the Trapezoid Rule function

e 6 t (50 sin 9 t)

Using the Trapezoid Rule, arriving at a global error thats less than 5% was found to
require the following n and h values:

n=359

h=

50
=0.01392757660
359

Global Error=4.927706

The Trapezoid Rule function, with n=359 was plotted against the exact solution and is
displayed as follows.

Image 3: Solution using Trapezoid Rule


with n=359

See Appendix A8 for the MATLAB code


Using the same Trapezoid Rule, arriving at a global error thats less than 0.01%
was found to require the following n and h values:
n=2520
50
h=
=0.0019841269841
2520
Global Error= 0.09997760%

The Trapezoid Rule function, with n=2520 was plotted against the exact solution and is
displayed as follows.

Image 4: Solution using Eulers Method


with n=2520

See Appendix A9 for the MATLAB code


Part D)
Next I plotted both my Eulers Method function and Trapezoid Rule function, at ~5%
error, against my exact solution in a single plot. The three functions are very close to
each other, and are fairly hard to distinguish.

See Appendix A10 for the MATLAB code

Appendices
Appendix A1
function f=f(x,t)
%f=dx(t)/dt=50sin(9t)-6x(t)
f=50*sin(9*t)-6*x;

Appendix A2
function fx=fx(t)
%fx=exact solution for x(t)
fx=76*exp(-6*t)/13+100*sin(9*t)/39-50*cos(9*t)/13;

Appendix A3
clear all
clc;
%Euler's Formula with n value to arrive at a global error <5%
%Requires function f.m
t01=0; %initial time
tn1=5; %final time
n1=8724; %number of time steps required for global error <5%, in this case 4.999861%
x01=2; %initial condition x(0)=2
dt1=(tn1-t01)/n1; %Step size, in this case it is 5.7313e-0
t1(1)=t01; %initial t-value
x1(1)=x01; %initial x-value
for N1=1:n1 %iterative loop
t1(N1+1)=t1(N1)+dt1;
x1(N1+1)=x1(N1)+dt1*f(x1(N1),t1(N1));
end
t=0:0.000001:5; %t-value for exact solution
x=76*exp(-6*t)/13+100*sin(9*t)/39-50*cos(9*t)/13; %x-value for exact solution
global_error1=(abs(fx(5)-x1(N1+1))/fx(5))*100;
disp('------------------------------------------------------------------------------------------------');
fprintf('Aaron Steinhoff \n 999326509 \n Part B \n');
disp('------------------------------------------------------------------------------------------------');
fprintf('\n The exact solution x(t), evaluated at 5 is:\nx(5)=%d:\n',fx(5));
fprintf('\n Using n=%d, the Eulers Method function f(t), evaluated at 5 is:\nf(5)=
%d:\n',n1,x1(N1+1));
fprintf('\n The global error, at n=%d is:\nabs((x(5)-f(5))/x(5))*100=
%d:\n',n1,global_error1);
fprintf('------------------------------------------------------------------------------------------------ \n');
hold on;
plot(t,x, 'g'); %plotting exact solution
plot(t1,x1,':'); %plotting euler's function with <5% error
xlabel ('t')
ylabel ('x(t)')
leg=legend('Exact Solution', 'Eulers Function with n=8724', 'Location', 'NorthWest');
title('Eulers Method with 4.999861% Error');

Appendix A4
%Euler's Formula with n value to arrive at a global error <0.01%
%Requires function f.m
%Requires function fx.m
t02=0; %initial time
tn2=5; %final time
ne2=436000; %number of time steps required for global error <0.1%, in this case 0.0999%
x02=2; %initial condition x(0)=2
dt2=(tn2-t02)/ne2; %Step size, in this case it is 1.1468e-05
t2(1)=t02; %initial t-value
x2(1)=x02; %initial x-value
for N2=1:ne2 %iterative loop
t2(N2+1)=t2(N2)+dt2;
x2(N2+1)=x2(N2)+dt2*f(x2(N2),t2(N2));
end
t=0:0.000001:5; %t-value for exact solution
x=76*exp(-6*t)/13+100*sin(9*t)/39-50*cos(9*t)/13; %x-value for exact solution
global_error2=(abs(fx(5)-x2(N2+1))/fx(5))*100;
disp('------------------------------------------------------------------------------------------------');
fprintf('Aaron Steinhoff \n 999326509 \n Part B \n');
disp('------------------------------------------------------------------------------------------------');
fprintf('\nThe exact solution x(t), evaluated at 5 is:\nx(5)=%d:\n',fx(5));
fprintf('\nUsing n=%d, the Eulers Method function f(t), evaluated at 5 is:\nf(5)=
%d:\n',ne2,x2(N2+1));
fprintf('\nThe global error, at n=%d is:\nabs((x(5)-f(5))/x(5))*100=
%d:\n',ne2,global_error2);
fprintf('------------------------------------------------------------------------------------------------ \n');
hold on;
plot(t,x, 'g'); %plotting exact solution
plot(t2,x2, ':'); %plotting euler's function with <0.01% error
xlabel ('t')
ylabel ('x(t)')
leg=legend('Exact Solution', 'Eulers Function with n=436000', 'Location', 'NorthWest');
title('Eulers Method with 0.09992173% Error');

Appendix A5
function ftx=ftx(t)
ftx=2*exp(-6*t)+50*exp(-6*t)*(3+exp(30)*(2*sin(45)-3*cos(45)))/39;
end

Appendix A6
function trap=trap(f,n,h) %Trapezoid Rule Function
summation=f(1); %initial value, from input parameter f
for j=2:(n) %loop
summation=summation+2*f(j);
end
summation=summation+2*f(n+1);
trap=h*summation/2 %function output
end

Appendix A7

function ft=ft(t)
ft=exp(6*t)*50*sin(9*t); %Integrand from Equation 2.21
end

Appendix A8
%Trapezoid Formula with n value to arrive at a global error <5%
%Requires function ft.m
%Requires function ftx.m
clear all;
clc;
a1=0; %Initial Value
b1=5; %Final Value
n1=359; %Number of Iteratiosn
h=(b1-a1)/n1; %Width of Trapezoid
t1(1)=a1;
t2=a1:0.000001:b1;
F1(1)=0;
zoid1(1)=0;
for i = 1:(n1);
t1(i+1)= t1(i)+ h ;
F1(i+1)= F1(i)+ h/2 *(ft(t1(i))+ft(t1(i+1)));
zoid1(i+1)=exp(-6*t1(i+1))*F1(i+1)+ 2*exp(-6*t1(i+1));
end
tr_calc1= zoid1(n1+1);
g_error1=(abs(fx(5)-tr_calc1)/fx(5))*100;
disp('------------------------------------------------------------------------------------------------');
fprintf('Aaron Steinhoff \n 999326509 \n Part C \n');
disp('------------------------------------------------------------------------------------------------');
fprintf('\nUsing n=%d, the Trapezoid Rule function f(t), evaluated at 5 is:\n f(5)=
%d:\n',n1,tr_calc1);
fprintf('\nThe exact solution x(t), evaluated at 5 is:\n x(5)=%d:\n',fx(5));
fprintf('\nThe global error, at n=%d is:\nabs((x(5)-f(5))/x(5))*100= %d:\n',n1, g_error1);
fprintf('------------------------------------------------------------------------------------------------ \n');
axis ( [0 5 -15 15] );
x1= fx(t2);
x2= zoid1 ;
hold on;
plot(t2,x1,'g');
plot(t1,x2,'--b');
title('Trapezoid Method with 4.927706% Error');
xlabel('t');
ylabel('x(t)');
eleg= legend('Exact Solution', 'Trapezoid Function with n=359');
%-----------------------------------------------------------------------------------------------------------------

Appendix A9
%Trapezoid Formula with n value to arrive at a global error <0.01%
%Requires function ft.m
%Requires function ftx.m
clear all;
clc;
a2=0; %Initial Value
b2=5; %Final Value
n2=2520; %Number of Iteratiosn
h2=(b2-a2)/n2; %Width of Trapezoid
t4(1)=a2;
t3=a2:0.000001:b2;
F2(1)=0;
zoid2(1)=0;
for i = 1:(n2);
t4(i+1)= t4(i)+ h2 ;
F2(i+1)= F2(i)+ h2/2 *(ft(t4(i))+ft(t4(i+1)));
zoid2(i+1)=exp(-6*t4(i+1))*F2(i+1)+ 2*exp(-6*t4(i+1));
end
tr_calc2= zoid2(n2+1);
g_error2=(abs(fx(5)-tr_calc2)/fx(5))*100;
disp('------------------------------------------------------------------------------------------------');
fprintf('Aaron Steinhoff \n 999326509 \n Part C \n');
disp('------------------------------------------------------------------------------------------------');
fprintf('\nUsing n=%d, the Trapezoid Rule function f(t), evaluated at 5 is:\n f(5)=
%d:\n',n2,tr_calc2);
fprintf('\nThe exact solution x(t), evaluated at 5 is:\n x(5)=%d:\n',fx(5));
fprintf('\nThe global error, at n=%d is:\nabs((x(5)-f(5))/x(5))*100= %d:\n',n2, g_error2);
fprintf('------------------------------------------------------------------------------------------------ \n');
axis ( [0 5 -15 15] );
x3= fx(t3);
x4= zoid2 ;
hold on;
plot(t3,x3,'g');
plot(t4,x4,'--b');
title('Trapezoid Method with 0.09997760% Error');
xlabel('t');
ylabel('x(t)');
eleg= legend('Exact Solution', 'Trapezoid Function with n=2520');
%-----------------------------------------------------------------------------------------------------------------

Appendix A10
%Part D)
%Euler's Formula with n value to arrive at a global error <5%
%Requires function f.m
clear all
clc;
t011=0; %initial time
tn11=5; %final time
n11=8724; %number of time steps required for global error <5%, in this case 4.999861%
x011=2; %initial condition x(0)=2
dt11=(tn11-t011)/n11; %Step size, in this case it is 5.7313e-04
t11(1)=t011; %initial t-value
x11(1)=x011; %initial x-value
for N11=1:n11 %iterative loop
t11(N11+1)=t11(N11)+dt11;
x11(N11+1)=x11(N11)+dt11*f(x11(N11),t11(N11));
end
t111=0:0.000001:5; %t-value for exact solution
x111=76*exp(-6*t111)/13+100*sin(9*t111)/39-50*cos(9*t111)/13; %x-value for exact solution
global_error11=(abs(fx(5)-x11(N11+1))/fx(5))*100;
disp('------------------------------------------------------------------------------------------------');
fprintf('Aaron Steinhoff \n 999326509 \n Part D \n');
disp('------------------------------------------------------------------------------------------------');
fprintf('\n The exact solution x(t), evaluated at 5 is:\nx(5)=%d:\n',fx(5));
disp('------------------------------------------------------------------------------------------------');
fprintf('\n Using n=%d, the Eulers Method function f(t), evaluated at 5 is:\nf(5)=
%d:\n',n11,x11(N11+1));
fprintf('\n The global error, at n=%d is:\nabs((x(5)-f(5))/x(5))*100=
%d:\n',n11,global_error11);
fprintf('------------------------------------------------------------------------------------------------ \n');
hold on;
%--------------------------------------------------------------------------------------------------------%Trapezoid Formula with n value to arrive at a global error <5%
%Requires function ft.m
%Requires function ftx.m
a1=0; %Initial Value
b1=5; %Final Value
n1=359; %Number of Iteratiosn
h=(b1-a1)/n1; %Width of Trapezoid
t1(1)=a1;
t2=a1:0.000001:b1;
F1(1)=0;
zoid1(1)=0;
for i = 1:(n1);
t1(i+1)= t1(i)+ h ;
F1(i+1)= F1(i)+ h/2 *(ft(t1(i))+ft(t1(i+1)));
zoid1(i+1)=exp(-6*t1(i+1))*F1(i+1)+ 2*exp(-6*t1(i+1));
end
tr_calc1= zoid1(n1+1);
g_error1=(abs(fx(5)-tr_calc1)/fx(5))*100;
fprintf('\nUsing n=%d, the Trapezoid Rule function f(t), evaluated at 5 is:\n f(5)=
%d:\n',n1,tr_calc1);
%fprintf('\nThe exact solution x(t), evaluated at 5 is:\n x(5)=%d:\n',fx(5));
fprintf('\nThe global error, at n=%d is:\nabs((x(5)-f(5))/x(5))*100= %d:\n',n1, g_error1);

fprintf('------------------------------------------------------------------------------------------------ \n');
axis ( [0 5 -15 15] );
x1= fx(t2);
x2= zoid1 ;
hold on;
%plot(t2,x1,'g');
plot(t111,x111, '.g'); %plotting exact solution
plot(t1,x2,'.-b'); %plotting trap function with <5% error
plot(t11,x11,'-r'); %plotting euler's function with <5% error
title('Comparison of Eulers Method and Trapezoid Function');
xlabel('t');
ylabel('x(t)');
eleg= legend('Exact Solution', 'Trapezoid Function with n=359', 'Eulers Method with
n=8724');
%-----------------------------------------------------------------------------------------------------------------

Das könnte Ihnen auch gefallen