Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. % Input: interval inter, initial value y0, number of steps n
  2. % Output: time steps t, solution y
  3. % Example usage: midpoint([0 1],1,10);
  4. function [t,y]=midpoint(inter,y0,n)
  5. t(1)=inter(1); y(1)=y0;
  6. h=(inter(2)-inter(1))/n;
  7. correct(1) = 1;
  8. for i=1:n
  9. t(i+1)=t(i)+h;
  10. disp("t value: " + t(i+1));
  11. y(i+1)=midpointstep(t(i),y(i),h);
  12. disp("Midpoint approx: " + y(i+1));
  13. error=abs(y(i+1)-(exp((1/3)*t(i+1)^3)));
  14. disp("Error " + error);
  15. disp(" ");
  16. end
  17. plot(t,y)
  18. function y=midpointstep(t,y,h)
  19. %one step of Euler’s Method
  20. %Input: current time t, current value y, stepsize h
  21. %Output: approximate solution value at time t+h
  22. s1 = ydot(t,y);
  23. s2 = ydot(t+h/2, y+h*s1/2);
  24. y=y+h*s2;
  25. function z=ydot(t,y)
  26. %right-hand side of differential equation
  27. z=t^2*y;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement