0

I need to solve this system of differential equations.

I tested it with removing rk(3) from the rk(2) equation and in that case I do get some solution. The code runs without error. However when I keep the rk(3) in the rk(2) equation I get a bunch of errors.

function rk = odes(t,y)

sigma1=sqrt(10e5);
sigma2=0.1;
sigma0=10e5;
m=1;k=2;vb=0.1;mis=0.15;mik=0.1;g=9.81;Fn=m*g;Fs=mis*Fn;Fc=mik*Fn;vs=0.001


rk(1)=y(2);
rk(2)=1/m*(sigma1*rk(3)+sigma0*y(3)+sigma2*vb-y(2)*(k+sigma2));
rk(3)=(vb-y(2))-((sigma0*(vb-y(2)))/(Fc+(Fs-Fc)*exp(-((vb-y(2)/vs)^2))));
rk=rk(:);

end
clc
close all
clear all


timerange=[0 20]
IC=[0.1;0;0.1]
[t,y]=ode45(@(t,y) odes(t,y),timerange,IC)

figure
plot(t,y(:,1));

errors

5
  • 2
    Just exchange the lines so that you compute rk(3) before rk(2). It should not be necessary, but you can also insert a dummy declaration rk(2)=0, then rk(3)=..., then the proper assignment of rk(2). Commented Aug 20, 2019 at 15:27
  • If you swap the two line rk(3)<-->rk(2) don't forget to adjust the IC vector (IC= [0.1,0.1,0])
    – obchardon
    Commented Aug 20, 2019 at 16:02
  • @obchardon : There is a difference between changing the order of computation leaving indices etc. the same, and changing the order of the components in the state vector, where indeed one would also have to adapt the IC to the changed order. Commented Aug 20, 2019 at 16:07
  • LoL I tried this and it didn't work, but now it did! I must have done something wrong. Thank you. However, there is still some strange things. For example, the vector t is equal to 0 for most of the time, only in the end (maybe the last 50 values or so) start raising to 20. Do you know why that is? i.imgur.com/iyDnDnd.png Commented Aug 20, 2019 at 17:21
  • 1
    That is due to the variable step size of the ode45 solver. You can either change timerange to a time vector at which you want the solution, or interpolate t and y yourself after integration with a time vector of your choice.
    – rinkert
    Commented Aug 20, 2019 at 19:49

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.