MATLAB Answers

How do solve four variables with one equation using Runge-kutta?

1 view (last 30 days)
Jibeom Hong
Jibeom Hong on 10 Jun 2021
Commented: Jibeom Hong on 12 Jun 2021
clear
h= 0.001;
tfinal = 0.2;
t(1)=0; v(1)=3.388; lc(1)=0; z(1)=0;
f=@(t,v,lc,z) (((0.015*log((sqrt(0.015+z)+sqrt(z))/0.015^(3/2))+sqrt(z)*sqrt(z+0.015)-z+2*0.015*log(0.015+lc))/(2*log(0.015+lc)-2*log(0.015)))*9.81*z-998*(9.81*lc-0.5*v^2)*2*pi*(0.015^2)+0.11*9.81)/(0.11-1.38*pi*(0.015^2)*998*(log((sqrt((z-lc)^2)/sqrt((0.015^2)+(z-lc)^2)))-0.015))
for i=1:ceil(tfinal / h)
t(i+1)=t(i)+h;
k1=h*f(t(i),v(i),lc(i),z(i));
k2=h*f(t(i)+0.5*h, v(i)+0.5*k1*h, lc(i)+0.5*k1*h, z(i)+0.5*k1*h);
k3=h*f(t(i)+0.5*h, v(i)+0.5*k2*h, lc(i)+0.5*k2*h, z(i)+0.5*k2*h);
k4=h*f(t(i)+h, v(i)+k3*h, lc(i)+k3*h, z(i)+k3*h);
v(i+1)=v(i)+h/6*(k1+2*k2+2*k3+k4);
end
% 1 eq, 4variables
%im matlab Beginner, this is my code. who can i help me....

Accepted Answer

James Tursa
James Tursa on 11 Jun 2021
Edited: James Tursa on 11 Jun 2021
You have three state variables, namely v, lc, and z. But you only have code for updating v. You need code for updating lc and z also. You have a couple of choices. Either have three function handles like this:
f=@(t,v,lc,z) (code for vdot here)
g=@(t,v,lc,z) (code for lcdot here)
h=@(t,v,lc,z) (code for zdot here)
Or you could create a single vector function handle to contain all three derivatives:
f=@(t,v,lc,z) [vdot;lcdot;zdot]
You would need to fill in the code for vdot, lcdot, and zdot. Looks like maybe the code you currently have for f only has the derivative for vdot, but you need all three.
Then you need to modify the code in your loop to handle these extra variables also. How you modify your looping code will depend on which choice you make for your derivative functions.

More Answers (0)

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!