Here is problem 2 for reference only, don\'t solve this problem. Solve the probl
ID: 3817425 • Letter: H
Question
Here is problem 2 for reference only, don't solve this problem. Solve the problem above (problem 4).
Problem 4 Write a user-defined MATLAB function that solves a first-order ODE by applying the midpoint method. For function name and arguments use [x.yl odeMIDPOINTOODE,a,b,h,yIND. The dy input argument ODE is a name for the functionthat calculates dx It is a dummy name for the function that is imported into odeMIDPOINT. The arguments a and b define the domain of the solution, h is the step size, and yINI is the initial value. The output arguments, x and y, are vectors with the x and y coordinates of the solution. Use odeMIDPOINT to solve problem 2.Explanation / Answer
please refer below code
1) create odeMIDPOINT.m and paste below code
function [x y] = odeMIDPOINT(ODE,a,b,h,yINI)
halfh = h / 2;
x(1,:) = yINI;
y(1) = a;
n = (b - a) / h; %calulating number of intervals
for i = 1 : n
y(i+1) = y(i) + h;
z = x(i,:) + halfh * ODE(y(i),x(i,:));
x(i+1,:) = x(i,:) + h * ODE(y(i)+halfh,z);
end;
2) create test_mid.m and paste below code
clc
clear all
close all
ODE = inline('y * t^3 - 1.5 * y','t','y')
a = 0;
b = 2;
h = 0.5;
yINI = 1;
[x y] = odeMIDPOINT(ODE,a,b,h,yINI); %calling function
fprintf(1,'%f %f ', [x y']'); %printing column wise
Please refer below output
ODE =
Inline function:
ODE(t,y) = y * t^3 - 1.5 * y
1.000000 0.000000
0.536133 0.500000
0.346471 1.000000
0.415156 1.500000
1.591802 2.000000
>>