Advertisement
Guest User

Untitled

a guest
Dec 13th, 2015
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 0.60 KB | None | 0 0
  1. clear all;clc;
  2. nsteps = 500;
  3. soln = zeros(2,nsteps+1); %allocate space for soln vector
  4. soln(:,1)  = [1,2]; % whatever x0 and y0 initial conditions are
  5.  
  6. t(1) = 0;
  7. for n = 1:nsteps
  8.     zn = soln(:,n);   % solution from previous timestep
  9.     D = 10;           % whatever parameter D is
  10.     dt = 0.1;         % timestep size
  11.    
  12.     % nonlinear equation solver to find next time step value
  13.     soln(:,n+1) = fsolve(@(znp1) (znp1(2)-zn(2))/dt + zn(2)/D - (znp1(1)-zn(1))/dt, soln(:,n) );
  14.     t(n+1) = t(n) + dt; % increment time for a counter
  15. end
  16. plot(t,soln(1,:),t,soln(2,:)); legend('x(t)','y(t)')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement