Advertisement
Guest User

Euler Code for Project 1.4 - Varun and Shaurya

a guest
Apr 22nd, 2019
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scilab 1.06 KB | None | 0 0
  1. funcprot(0);
  2. clc();
  3. clf();
  4.  
  5. initialX = 0;
  6. initialY = 550315.7725;
  7. stepSize = 1;
  8. numberIterations = 300;
  9. colorVar = 3;
  10. solFind = 260; //time at which you want to find the population
  11.  
  12. function [dy] = f(x, y)
  13.     dy = 0.02808621325311*y*(1-y/21172402.245108);
  14. endfunction
  15.  
  16. function [x, y] = euler(f, x1, y1, h, n, l)
  17.     x(1) = x1;
  18.     y(1) = y1;
  19.     for i = 1:n
  20.         x(i + 1) = x(i) + h;
  21.         y(i + 1) = y(i) + h * f(x(i), y(i))
  22.     end
  23. plot2d(x, y, style = l+3);
  24. disp(y(solFind),  " is ", solFind,"Approximation at t = ");
  25. xgrid();
  26. endfunction
  27.  
  28. function [a, b, q] = actual(f, x1, y1, h, n, c)
  29.     for i =1:n
  30.         a(i) = h * i;
  31.         b(i) = ode(y1, x1, h*i, f);
  32.     end
  33. plot2d(a, b, style = c);
  34. disp(b(solFind),  " is ", solFind,"Actual value at t = ");
  35. xtitle("Population of New York versus time");
  36. xlabel("Time");
  37. ylabel("Population");
  38. legend('Euler approximation', 'Exact solution', 2);
  39. endfunction
  40.  
  41. euler(f, initialX, initialY, stepSize, numberIterations, colorVar);
  42. actual(f, initialX, initialY, stepSize, numberIterations, colorVar);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement