Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- funcprot(0);
- clc();
- clf();
- initialX = 0;
- initialY = 550315.7725;
- stepSize = 1;
- numberIterations = 300;
- colorVar = 3;
- solFind = 260; //time at which you want to find the population
- function [dy] = f(x, y)
- dy = 0.02808621325311*y*(1-y/21172402.245108);
- endfunction
- function [x, y] = euler(f, x1, y1, h, n, l)
- x(1) = x1;
- y(1) = y1;
- for i = 1:n
- x(i + 1) = x(i) + h;
- y(i + 1) = y(i) + h * f(x(i), y(i))
- end
- plot2d(x, y, style = l+3);
- disp(y(solFind), " is ", solFind,"Approximation at t = ");
- xgrid();
- endfunction
- function [a, b, q] = actual(f, x1, y1, h, n, c)
- for i =1:n
- a(i) = h * i;
- b(i) = ode(y1, x1, h*i, f);
- end
- plot2d(a, b, style = c);
- disp(b(solFind), " is ", solFind,"Actual value at t = ");
- xtitle("Population of New York versus time");
- xlabel("Time");
- ylabel("Population");
- legend('Euler approximation', 'Exact solution', 2);
- endfunction
- euler(f, initialX, initialY, stepSize, numberIterations, colorVar);
- actual(f, initialX, initialY, stepSize, numberIterations, colorVar);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement