Modify Euler's Method to Heun's method
Afficher commentaires plus anciens
Hey all i have coded Eulers method, however i now need to modify it to include Heun's method this is what i have so far...
% % This is the work of Dr. Gretarson
% % Damped Simple Harmonic Oscillator solved by Euler's Method
% %
% % This routine implements a numerical (Euler's Method) solution to the
% % damped simple harmonic oscillator.
% % Physical Parameters of the Oscillator
% w0=10; beta=1;
% w=sqrt(w0^2-(beta/2)^2);
% phi=atan(-1/20);
% x0=1/cos(phi);
% % Parameters for the Integration
% h = 0.003; % Time step in seconds
% duration=15; % Duration of solution (seconds)
% Nstep=round(duration/h); % Resulting number of steps needed.
% t=0:h:Nstep*h; % Resulting time vector (seconds)
% % Reserve some memory for the solution vector (these lines are optional but
% % do speed up the code.)
% x=zeros(size(t)); % x is position (meters)
% y=zeros(size(t)); % y is velocity (meters)
% % Set the initial conditions to be the first elements of x and y.
% x(1)=x0; % Initial position (meters)
% y(1)=0; % Initial velocity (meters/second)
% % Iterate to find solution with Euler's method
% for n=2:Nstep
%
% f=y(n-1); % f(x,y,t) is dx/dt
% x(n)=x(n-1)+f*h; % Use the last position to find the new position
% g=-beta*y(n-1)-w0^2*x(n-1); % g(x,y,t) is dy/dt.
% y(n)=y(n-1)+g*h; % Use the last velocity to find the new velocity
%
% slopery = (y(n)+h*(x(n)));
%
% sloperx = x(n)+h;
%
% ideal = (1/2)*(sloperx + slopery);
%
%
% end
% % The exact solution (solved on paper) is
% xexact=x(1)*exp(-beta/2*t).*cos(w*t+phi);
% yexact=x(1)*(-beta/2*exp(-beta/2*t).*cos(w*t+phi)-w*exp(-beta/2*t).*sin(w*t+phi));
% GDE=max(xexact-x);
% % Compare the exact solution and the numerical solution
% figure(1);
% plot(t,x,'bo',t,xexact,'r','linewidth',1,'markersize',2); hold on;
% title(['GDE = ',num2str(GDE)]);
% grid on; hold off;
% % =======================================================================
% figure(2)
% plot(t,ideal,'bo',t,xexact,'r')
3 commentaires
Geoff Hayes
le 10 Oct 2015
Rob - what is your question and why is all of your code commented out?
Rob Mullins
le 10 Oct 2015
Modifié(e) : Walter Roberson
le 11 Oct 2015
Rob Mullins
le 10 Oct 2015
Modifié(e) : Rob Mullins
le 10 Oct 2015
Réponses (0)
Catégories
En savoir plus sur Programming dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!