Runge Kutta 4th Order Help
7 views (last 30 days)
Show older comments
So I'm having some trouble with my Runge-Kutta 4th order ODE, and I'm not really sure what I'm doing wrong with my code. The output for the y-values that I'm getting seem to be going off into infinity, and I'm not able to find where this error is happening. The k1-k4 values that I'm getting also seem to be ridiculously high, and I'm not sure why that is. Any help would be appreciated, thanks!
function [t,r]=mathmodelingproject1(Vm,Oeff)
%Defining Variables
D=5*10^(-14);
kb=1.38*10^(-23);
T=310;
Fmax=0.7*10^(-9);
rh=0.97*10^(-9);
rt=0.31*10^(-9);
B=1.4*10^(-19);
rs=0.51*10^(-9);
gamma=1.8*10^(-11);
%RK4
r0=0.5*10^-9; %initial condition
r=r0;
t=0;
h=0.0001; %Step Size
%Calculation Loop
for t=0:h:0.03
plot(t,r)
hold on
dr=@(r,Vm,Oeff) [[D/(kb*T)]*[(Vm^2*Fmax)/(1+(rh/(r+rt)))]+[(4*B)*((rs/r)^4)*
(1/r)]-2*pi*gamma+2*pi*Oeff*r]; %Function
k1=dr(r,Vm,Oeff)*h;
k2=dr(r+k1/2,Vm,Oeff)*h;
k3=dr(r+k2/2,Vm,Oeff)*h;
k4=dr(r+k3,Vm,Oeff)*h;
r=r+(k1+2*k2+2*k3+k4)/6;
end
xlabel('Time');
ylabel('Pore Diameter');
title('RK4 Manual Solution');
end
1 Comment
Jan
on 2 Oct 2017
Edited: Jan
on 2 Oct 2017
Note: 5e-14 is nicer and cheaper than 5*10^(-14).
Square brackets are the concatenation operator in Matlab. It is not useful to apply them "just for fun" or instead of round parenthesis.
Please provide teh values for the inputs Vm and Oeff, such that we can reproduce your results.
Check your formula for dr.
Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!