Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Programming Assignment
Aaron Steinhoff
Instructor: Professor H. Montazeri
Aaron Steinhoff
( )
( )
( )
( )
Homogenous Solution
( )
Particular Solution
( )
( )
( )
( )
( )
( )
( )
( )
( )
( )
( )
( )
( )
( )
( )
( )
( )
( )
( )
( )
( )
( )
( )
( )
( )
Part B)
( )
To arrive at a global error that was less than 5%, a value and subsequent
established, through trial and error. These values are as follows:
value was
To arrive at a global error that was less than 0.01%, following the same steps, the subsequent
values are as follows:
( )
( )
( )
( )
( )
Exact Solution:
( )
))
( )
( )
))
))
), which is used
Using the Trapezoid Rule, arriving at a global error thats less than 5% was found to require the
following and h values:
Using the same Trapezoid Rule, arriving at a global error thats less than 0.01% was found to
require the following and h values:
0.09997760%
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.
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 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
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 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 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 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');
%-----------------------------------------------------------------------------------------------------------------