Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- % Input: interval inter, initial value y0, number of steps n
- % Output: time steps t, solution y
- % Example usage: midpoint([0 1],1,10);
- function [t,y]=midpoint(inter,y0,n)
- t(1)=inter(1); y(1)=y0;
- h=(inter(2)-inter(1))/n;
- correct(1) = 1;
- for i=1:n
- t(i+1)=t(i)+h;
- disp("t value: " + t(i+1));
- y(i+1)=midpointstep(t(i),y(i),h);
- disp("Midpoint approx: " + y(i+1));
- error=abs(y(i+1)-(exp((1/3)*t(i+1)^3)));
- disp("Error " + error);
- disp(" ");
- end
- plot(t,y)
- function y=midpointstep(t,y,h)
- %one step of Euler’s Method
- %Input: current time t, current value y, stepsize h
- %Output: approximate solution value at time t+h
- s1 = ydot(t,y);
- s2 = ydot(t+h/2, y+h*s1/2);
- y=y+h*s2;
- function z=ydot(t,y)
- %right-hand side of differential equation
- z=t^2*y;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement