Professional Documents
Culture Documents
ME 303 Homework 5
By: Joseph Apollo
clc
clear all
close all
Problem 1b
fprime = @(t) exp(-1.*(t).^2); %anonymous function for function in
intergral
I = quadl(fprime,0,2) %quadl function that evaluates f on bounds which
are 0 to 2
I =
0.882082140308872
Problem 3
fun = @(x,y) x.^2 .* y - 1.1.*y; %function from problem 2 where t is
replaced by x
a = 0; %initial t value
b = 2; %final t value
y0 = 1; %initial value of y at x = a
h = .25; %step size
x3 =
Columns 1 through 3
0 0.250000000000000 0.500000000000000
1
Columns 4 through 6
Columns 7 through 9
y3 =
Columns 1 through 3
Columns 4 through 6
Columns 7 through 9
Problem 4b,4c,4d
fun = @(x,y) -2.5 .* y; %function from problem 4
a = 0; %initial x value
b = 3.4; %final x value
y0 = 1; %initial y value at x = a
% h = .2
[c,d] = euler(fun,a,b,y0,.2) %evaluates all points on interval a:h:b
for h = .2
% h = .85
[e,f] = euler(fun,a,b,y0,.85) %evaluates all points on interval a:h:b
for h = .85
figure(1)
plot(x1,y1) %plots and labels analytic solution and all data points
for h=.2 and h=.85
hold on
plot(c,d,'go')
hold on
plot(e,f,'ro')
grid on
title('Problem 4 analytic, h=0.2, h=0.85')
xlabel('x values')
ylabel('y values')
legend('Analytic','h=0.2','h=0.85','Location','northwest')
2
c =
Columns 1 through 3
0 0.200000000000000 0.400000000000000
Columns 4 through 6
Columns 7 through 9
Columns 10 through 12
Columns 13 through 15
Columns 16 through 18
d =
Columns 1 through 3
Columns 4 through 6
Columns 7 through 9
Columns 10 through 12
Columns 13 through 15
Columns 16 through 18
3
e =
Columns 1 through 3
0 0.850000000000000 1.700000000000000
Columns 4 through 5
2.550000000000000 3.400000000000000
f =
Columns 1 through 3
Columns 4 through 5
-1.423828125000000 1.601806640625000
Problem 5b, 5c
format long
4
xspan = [0 6]; %starting and final values on interval z
y0 = [0 0 .3326]; %y0 vector for initial values of 3 functions derived
in part a
figure(2)
plot(x120,a,'g-',x120,b,'r-',x120,c,'b-') %plots and labels f, f', and
f'' vs z and labels graph
title('Graph of function derivatives vs z')
xlabel('z value')
ylabel('Function value for f, fprime, and f2prime')
grid on
legend('f','fprime','f2prime','Location','NorthWest')
j =
4.292864857391530
k =
4.287133242959754
l =
4.285695960378460
5
Published with MATLAB® R2021a