You are on page 1of 6

Table of Contents

ME 303 Homework 5 .......................................................................................................... 1


Problem 1b ........................................................................................................................ 1
Problem 3 .......................................................................................................................... 1
Problem 4b,4c,4d ................................................................................................................ 2
Problem 5b, 5c ................................................................................................................... 4

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,y3] = euler(fun,a,b,y0,h) %evaluates number 2 to compare with hand


calculations

%Function "euler" in back of pdf

x3 =

Columns 1 through 3

0 0.250000000000000 0.500000000000000

1
Columns 4 through 6

0.750000000000000 1.000000000000000 1.250000000000000

Columns 7 through 9

1.500000000000000 1.750000000000000 2.000000000000000

y3 =

Columns 1 through 3

1.000000000000000 0.725000000000000 0.536953125000000

Columns 4 through 6

0.422850585937500 0.366030038452148 0.356879287490845

Columns 7 through 9

0.398143455106974 0.512609698450228 0.764108831752372

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

x1 = a:.01:b; %creates enough x values to generate smooth curve


y1 = exp(-2.5.*x1); %analytic solution from 4a

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

0.600000000000000 0.800000000000000 1.000000000000000

Columns 7 through 9

1.200000000000000 1.400000000000000 1.600000000000000

Columns 10 through 12

1.800000000000000 2.000000000000000 2.200000000000000

Columns 13 through 15

2.400000000000000 2.600000000000000 2.800000000000000

Columns 16 through 18

3.000000000000000 3.200000000000000 3.400000000000000

d =

Columns 1 through 3

1.000000000000000 0.500000000000000 0.250000000000000

Columns 4 through 6

0.125000000000000 0.062500000000000 0.031250000000000

Columns 7 through 9

0.015625000000000 0.007812500000000 0.003906250000000

Columns 10 through 12

0.001953125000000 0.000976562500000 0.000488281250000

Columns 13 through 15

0.000244140625000 0.000122070312500 0.000061035156250

Columns 16 through 18

0.000030517578125 0.000015258789062 0.000007629394531

3
e =

Columns 1 through 3

0 0.850000000000000 1.700000000000000

Columns 4 through 5

2.550000000000000 3.400000000000000

f =

Columns 1 through 3

1.000000000000000 -1.125000000000000 1.265625000000000

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

[x30, y30] = midpointsys(@prob5, xspan, y0, 30); %solves system for n


= 30
[x60, y60] = midpointsys(@prob5, xspan, y0, 60); %solves system for n
= 60
[x120, y120] = midpointsys(@prob5, xspan, y0, 120); %solves system for
n = 120

j = y30(length(x30)) %value at z = 6 for n = 30


k = y60(length(x60)) %value at z = 6 for n = 60
l = y120(length(x120)) %value at z = 6 for n = 120

a = y120(:,1); %takes first column of the matrix y120


b = y120(:,2); %takes second column of matrix y120
c = y120(:,3); %takes third column of matrix y120

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

%Function "prob5" in back of pdf

j =

4.292864857391530

k =

4.287133242959754

l =

4.285695960378460

5
Published with MATLAB® R2021a

You might also like