Transcript
Page 1: CME362 Programming Assignment

Aaron Steinhoff

Instructor: Professor H. Montazeri

Page 2: CME362 Programming Assignment

CME362 Linear Programming Assignment Aaron Steinhoff

Part A) First order, inhomogeneous, linear ODE:

x (t )+αx (t )=β sin ct

α=6β=50c=9

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

Homogenous Solution Particular Solution

xh (t )+6 xh ( t )=0

⟹d xhdt

=−6 xh (t )

⟹ 1xh ( t )

d xh=−6 xh ( t )dt

⟹∫ 1xh ( t )

d xh=∫−6 ( t )dt

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

⟹ xh (t )=c e−6 t

x p (t )=k1sin 9t+k2 cos9 t

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

(k1 sin 9 t+k2 cos9 t )+6 (k1 sin 9 t+k2cos 9 t )

¿50sin 6 t⟹9k1 cos9 t−9k 2sin 9 t+6k1 sin 9 t+6 k2cos 9 t=50sin6 t

⟹ (6k1−9k2 ) sin 9 t+(9k1+6k2 )cos 9t=50sin 6 t

{6k1−9k2=509k1+6k 2=0⇒{k1=10039k2=

−5013

→xp (t )=10039sin 9 t−50

13cos 9t

x (t )=xh (t )+x p (t )

→x ( t )=c e−6 t+ 10039sin 9 t−50

13cos 9t

x (0 )=c e−6 (0 )+ 10039sin 9(0)−50

13cos 9(0)=2

⟹ c−5013

=2⟹ c=7613

x (t )=7613e−6 t+ 100

39sin 9 t−50

13cos 9t

Page 3: CME362 Programming Assignment

x (5 )=7613e−6 (5)+100

39sin 9 (5 )−50

13cos 9 (5 )

¿ 7613e−30+100

39sin 45−50

13cos 45

¿0.1613347213018903451150580163171508248958632701187105…[¿ . solution ]

Part B)

o See Appendix A1 for MATLAB function for x (t )=50sin 9 t−6 x ( t )

o See Appendix A2 for MATLAB function for exact solution

x (t )=7613e−6 t+ 100

39sin 9 t−50

13cos 9t

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= 5−08724

=5.73131591∗10−4

Global Error=4.999861%

Page 4: CME362 Programming Assignment

The Euler’s Method function, withn=8724was plotted against the exact solution and is displayed as follows.

Image 1: Solution using Euler’s 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 subsequent n ∆ t values are as follows:

n=436000

∆ t= 5−0436000

=1.146788991∗10−5

Global Error=0.0999%

Page 5: CME362 Programming Assignment

The Euler’s Method function, withn=436000 was plotted against the exact solution and is displayed as follows.

Image 2: Solution using Euler’s Method with n=436000

See Appendix A4 for the MATLAB code.

Page 6: CME362 Programming Assignment

Part C) Equation 2.21 from Bill Goodwine’s Engineering Differential Equations:

x (t )=e−αt∫t0

tn

eαtg ( t )dt+x0 eα t0 e−αt

g ( t )=50sin 9 t

x0=x (0 )=2

α=6

→x ( t )=e−6 t∫0

5

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

¿e−6 t∫0

5

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

Exact Solution:

x (t )=2e−6 t+ 5039e−6 t (3+e30 (2sin 45−3cos 45 ) )

Evaluating the function at x (5):

x (5 )=2e−6(5)+ 5039e−6 (5)(3+e30 (2sin 45−3cos45 ) )=0.1613

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

o See Appendix A5 for MATLAB function for exact solution

x (t )=2e−6 t+ 5039e−6 t (3+e30 (2sin 45−3cos 45 ) )

o See Appendix A6 for MATLAB function for Trapezoid Rule

o See Appendix A7 for MATLAB function for the integrand, e6 t(50sin 9 t), which is used in the Trapezoid Rule function

Page 7: CME362 Programming Assignment

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

n=359

h=5−0359

=0.01392757660

Global Error=4.927706%

The Trapezoid Rule function, withn=359was 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

Page 8: CME362 Programming Assignment

Using the same Trapezoid Rule, arriving at a global error that’s less than 0.01% was found to require the following n and h values:

n=2520

h=5−02520

=0.0019841269841

Global Error=¿0.09997760%The Trapezoid Rule function, withn=2520 was plotted against the exact solution and is displayed as follows.

Image 4: Solution using Euler’s Method with n=2520

See Appendix A9 for the MATLAB code

Page 9: CME362 Programming Assignment

Part D)

Next I plotted both my Euler’s 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

Page 10: CME362 Programming Assignment

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 allclc;

%Euler's Formula with n value to arrive at a global error <5%%Requires function f.mt01=0; %initial timetn1=5; %final timen1=8724; %number of time steps required for global error <5%, in this case 4.999861%x01=2; %initial condition x(0)=2dt1=(tn1-t01)/n1; %Step size, in this case it is 5.7313e-0 t1(1)=t01; %initial t-valuex1(1)=x01; %initial x-valuefor N1=1:n1 %iterative loop t1(N1+1)=t1(N1)+dt1; x1(N1+1)=x1(N1)+dt1*f(x1(N1),t1(N1));endt=0:0.000001:5; %t-value for exact solutionx=76*exp(-6*t)/13+100*sin(9*t)/39-50*cos(9*t)/13; %x-value for exact solutionglobal_error1=(abs(fx(5)-x1(N1+1))/fx(5))*100;disp('-------------------------------------------------------------------------------------------------');fprintf('Aaron Steinhoff \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');

Page 11: CME362 Programming Assignment

Appendix A4

%Euler's Formula with n value to arrive at a global error <0.01%%Requires function f.m%Requires function fx.mt02=0; %initial timetn2=5; %final timene2=436000; %number of time steps required for global error <0.1%, in this case 0.0999%x02=2; %initial condition x(0)=2dt2=(tn2-t02)/ne2; %Step size, in this case it is 1.1468e-05t2(1)=t02; %initial t-valuex2(1)=x02; %initial x-valuefor N2=1:ne2 %iterative loop t2(N2+1)=t2(N2)+dt2; x2(N2+1)=x2(N2)+dt2*f(x2(N2),t2(N2));endt=0:0.000001:5; %t-value for exact solutionx=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 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 solutionplot(t2,x2, ':'); %plotting euler's function with <0.01% errorxlabel ('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 Functionsummation=f(1); %initial value, from input parameter f for j=2:(n) %loop summation=summation+2*f(j);endsummation=summation+2*f(n+1);trap=h*summation/2 %function outputend

Page 12: CME362 Programming Assignment

Appendix A7

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

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 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');%------------------------------------------------------------------------------------------------------------------

Page 13: CME362 Programming Assignment

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 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');%------------------------------------------------------------------------------------------------------------------

Page 14: CME362 Programming Assignment

Appendix A10

%Part D)%Euler's Formula with n value to arrive at a global error <5%%Requires function f.mclear allclc;t011=0; %initial timetn11=5; %final timen11=8724; %number of time steps required for global error <5%, in this case 4.999861%x011=2; %initial condition x(0)=2dt11=(tn11-t011)/n11; %Step size, in this case it is 5.7313e-04t11(1)=t011; %initial t-valuex11(1)=x011; %initial x-valuefor N11=1:n11 %iterative loop t11(N11+1)=t11(N11)+dt11; x11(N11+1)=x11(N11)+dt11*f(x11(N11),t11(N11));endt111=0:0.000001:5; %t-value for exact solutionx111=76*exp(-6*t111)/13+100*sin(9*t111)/39-50*cos(9*t111)/13; %x-value for exact solutionglobal_error11=(abs(fx(5)-x11(N11+1))/fx(5))*100;disp('-------------------------------------------------------------------------------------------------');fprintf('Aaron Steinhoff \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);

Page 15: CME362 Programming Assignment

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 solutionplot(t1,x2,'.-b'); %plotting trap function with <5% errorplot(t11,x11,'-r'); %plotting euler's function with <5% errortitle('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');%------------------------------------------------------------------------------------------------------------------