Вы находитесь на странице: 1из 7

MAE 305

01

Numerical Methods in MAE


Problem Set No. 4
04/27/2016

Kurzawa, Eryk
8159

Chapter 5 Problem 3

%Ch 5 problem 63
x = 0:20;
y = besselj(0,x);
xi = linspace(0, 20);
ysp = interp1(x, y, xi, 'spline');
%interpolated value at x = 3.5 using spline
yspi = spline(x,y,3.5)
%actual value
actual = besselj(0, 3.5)
plot(xi, ysp, x, y, 'o', 3.5, yspi, 'r*')
grid on
%Percent Difference
PercDiff = (abs(actual - yspi))/abs(actual) * 100

yspi =
-0.3791

actual =
-0.3801

PercDiff =
0.2587

Figure 1: Constructed cubic spline interpolation shown in blue. Red star indicates interpolated data point at x = 3.5,
and red circles indicate the data given.

Chapter 6 Problem 33
%create given data
x = 1:0.1:2;
y = [0.31 0.27 0.22 0.17 0.14 0.10 0.08 0.05 0.04 0.02 0.01];

function I = TrapComp_ESData(x,y);
%
%
I = TrapComp_ESData(x,y) where
%
%
x is the input data
%
y is the input data
%
n is the number of equal-length subintervals in [x(1),x(n+1)],
%
%
I is the integral estimate.
%
h = x(2)-x(1);
I = 0;
n = numel(x)-1; %number of sub intervals
for i = 2:n,
I = I + 2*y(i);
end
I = I + y(1) + y(n+1);
I = I*h/2;

>> I = TrapComp_ESData(x,y)
I =
0.1250

Chapter 6 Problem 56
function I = Boole_Comp(f,a,b,n)
%
% Boole_Comp estimates the value of the integral of f(x) from a to b
% by using the composite Boole's rule applied to n equal-length
% subintervals.
%
%
I = Boole_Comp(f,a,b,n) where
%
%
f is an inline function representing the integrand,
%
a and b are the limits of integration,
%
n is the number of equal-length subintervals in [a,b],
%
%
I is the integral estimate.
format long
h = (b-a)/n; I = 0;
x = a:h:b;
for i = 1:4:n
I = I + 7*f(x(i)) + 32*f(x(i+1)) + 12*f(x(i+2)) + 32*f(x(i+3)) + 7*f(x(i+4));
end
I = I*((2/45)*h);
>> format long
>> f = inline('(2*x-1)/(1+cos(x))');
>> I = Boole_Comp(f,0,1,20)
I =
0.023965528954473

Next, the MATLAB built in function quad is used to evaluate the function f.
>> F = @(x)(2*x-1)./(1+cos(x));
>> IQ = quad(F,0,1)
IQ =
0.023965532415080

We can see that theres a 3.461e-09 difference when comparing the two methods indicating that Boole_Comp is a
method thats fairly accurate.

Chapter 7 Problem 18
For this problem, we are required to write a MATLAB file script to solve the IVP given. First method we use is
EulerODE which has been given to the students and wont be mentioned shown. The second method we use is our
own function called Taylor_2_ODE which is a script file written in order to solve the first order IVP using secondorder Taylor method.
function yTaylor2ODE = Taylor_2_ODE(f,fp,x,y0)
%
% Taylor_2_ODE uses Taylor's method to solve a first-order ODE given in the form
% y' = f(x,y) subject to initial condition y0.
%
% y = Taylor_2_ODE(f,fp,x,y0) where
%
%
f is an inline function representing f(x,y),
%
fp is an inline function representing f'(x,y),
%
x is a vector representing the mesh points,
%
y0 is a scalar representing the initial value of y,
%
%
y is the vector of solution estimates at the mesh points.
yTaylor2ODE = 0*x;
yTaylor2ODE(1) = y0;

% Pre-allocate

h = x(2)-x(1);
for n = 1:length(x)-1
yTaylor2ODE(n+1) = yTaylor2ODE(n) + h*(f(x(n), yTaylor2ODE(n)) +
(1/2)*h*fp(x(n), yTaylor2ODE(n)));
end
end

disp('

yEuler

yTaylor2ODE

yExact

h = 0.05; x = 1:h:2; y0 = 0.75;


f = inline('log(x)-(y/x)', 'x', 'y');
fp = inline('(log(x)-(y/x))/x^2 + 1/x', 'x', 'y');
yEuler = EulerODE(f,x,y0);
yEu = yEuler(1:2:end);
h = 0.1; x = 1:h:2;
yTaylor2ODE = Taylor_2_ODE(f,fp,x,y0);
yExact = inline('((1/2)*x*log(x)) - ((1/4)*x)+(1/x)');
for k=1:length(x),
x_coord = x(k);
yE = yEu(k);
yT = yTaylor2ODE(k);
yEx = yExact(x(k));

e_Euler

e_Taylor2 ')

e_Euler = (yEx-yE)/yEx*100;
e_Taylor2 = (yEx-yT)/yEx*100;
fprintf('%6.2f %11.6f %11.6f
%11.6f
%6.2f
%6.2f\n',x_coord,yE,yT,yEx,e_Euler, e_Taylor2)
end

x
1.00
1.10
1.20
1.30
1.40
1.50
1.60
1.70
1.80
1.90
2.00

yEuler
0.750000
0.681011
0.633339
0.602580
0.585582
0.580019
0.584131
0.596556
0.616221
0.642266
0.673990

yTaylor2ODE
0.750000
0.676250
0.626703
0.595696
0.579376
0.575014
0.580608
0.594655
0.615992
0.643706
0.677063

yExact
0.750000
0.686512
0.642726
0.614768
0.599816
0.595765
0.601003
0.614269
0.634564
0.661077
0.693147

e_Euler
0.00
0.80
1.46
1.98
2.37
2.64
2.81
2.88
2.89
2.85
2.76

e_Taylor2
0.00
1.49
2.49
3.10
3.41
3.48
3.39
3.19
2.93
2.63
2.32

The results returned by the Taylor method become more accurate when x becomes larger in comparison to Eulers
method. If the range of x was changed from 1 to 3, the we would see a massive decrease in the relative error
percentage.

Вам также может понравиться