Runge Kutta Method Matlab Help

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

Jeff Carey
Jeff Carey 2015년 4월 29일
편집: Jeff Carey 2015년 4월 29일
I think i messed up myF function.. or some where along there. Thats where i keep getting my errors but not sure why or how to fix it

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

질문:

2015년 4월 29일

편집:

2015년 4월 29일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by