Runge Kutta Method Matlab Help
Mostrar comentarios más antiguos
Hey guys, I am suppose to be finding the 2nd and 4th order runge kutta method with delta x = 0.5 and initial conditions y(0)=1 and x=0 to x=2. The ODE is x(dy/dx)=y*x^3-1.2*y*x This is where i am now and i am stuck.
clear all
close all
clc
dt1=.5; % coarse independent variable step size
dt2=.2; % fine independent variable step size
dte=.001; % very fine timestep for exact(psuedo) curve
tmin=0; % beginning of time
tmax=2; % end of the time study
t1=tmin:dt1:tmax;
t2=tmin:dt2:tmax; %different length ranges because of different dt's
te=tmin:dte:tmax;
y1(1)=1; %initial conditions
y2(1)=1;
c_1=1; %unknown constant from initial condtion from y(1)=1 %cant use zero as index in matlab
myF = y1*t1.^3-1.2*y1*t1
% exact solution (approximation shown in graph at dt = .001) for i=1:length(te) ye(i) = c_1*exp(0.333333*te(i)^3-1.2*te(i)); end
% 2nd Order Runga Kutta - dt=.5 for i=1:length(t1)-1
k1=dt1*myF(t1(i),y1(i));
k2=dt1*myF(t1(i)+dt1,y1(i)+k1);
y1(i+1)=y1(i)+.5*k1+.5*k2;
end
% 2nd Order Runga Kutta - dt=.1 for i=1:length(t2)-1
k1=dt2*myF(t2(i),y2(i));
k2=dt2*myF(t2(i)+dt2,y2(i)+k1);
y2(i+1)=y2(i)+.5*(k1+k2);
end
figure(1)
subplot(1,2,1) %the figure has 1 row and 2 columns. the plot in 1 spot
plot(t1,y1,':rp');hold on
plot(t2,y2,'-bo');hold on
plot(te,ye,'m')
legend('dt=.5','dt=.1','exact')
xlabel('t');ylabel('y')
title('2^{nd} Order Runga-Kutta')%notice the fancy formatting
grid on
axis equal
axis([tmin, tmax, min([min(y1) min(y2) min(ye)]),...
max([max(y1) max(y2) max(ye)])]);
subplot(1,2,2) %the figure has 1 row and 2 columns. the plot in 2 spot plot(te,ye,'m')
legend('exact') xlabel('t');ylabel('y') title('4^{th} Order Runga-Kutta') grid on axis equal axis([0 2 0 1.5])
1 comentario
Jeff Carey
el 29 de Abr. de 2015
Editada: Jeff Carey
el 29 de Abr. de 2015
Respuestas (0)
Categorías
Más información sobre Numerical Integration and Differential Equations en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!