Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

0.4. Suppose you are given a Matlab function FTest) which takes scalar values of

ID: 3348539 • Letter: 0

Question

0.4. Suppose you are given a Matlab function FTest) which takes scalar values of t and y as its two arguments and returns two values; the first is f(t, y) and the second is its first derivative t, y). The numerical solution of the first differential en f(t, y) with initial value y(to)-o can be solved by the following formula h2 where tn-to + nh, n-1,.,N for some step size h, and yny(t. As suming that yo, to, N and h are already defined, write Matlab code to solve the differential equation, store the resulting values of y

Explanation / Answer

clc;
f=@(t,y) y^2+1; %(example problem)
f1=@(t,y)2*y*(y^2+1); %%% givin function dy/dt=f(t,y)=y^2+1 (example problem)
t(1)=0; %initial condotion
y(1)=0;
h=0.1 ;% step length
N=10;
for n=1:N
  
y(n+1)=y(n)+h*f(t(n),y(n))+h^2*f1(t(n),y(n))/2;
t(n+1)=t(1)+n*h;
end
y'

%%% Solution

ans =

0
0.100000000000000
0.202010000000000
0.308193340331806
0.421066318934197
0.543753203916197
0.680365192390769
0.836607912486357
1.020820797393602
1.245874234755924
1.532891730360954

>>