Using scilab to solve and plot differential equations

我只是一个虾纸丫 提交于 2019-12-12 04:56:20

问题


How can I solve the second order differential equation using scilab ode() function. (For example: y'' + 3y' +2y = f(x), y(0)=0, y'(0)=0) And then plot the result of function y(x).

I want to use this to model the RLC-circuit signal with step-function input

Here is the code I tried

function y=u(t)
    y=(sign(t)+1)/2
endfunction

L=0.001
R=10
C=0.000001

function zdot=f(t,y)
    zdot(1)= y(2);
    zdot(2)=(u(t)-y(1)-L*y(2)/R)/(L*C);
endfunction

y0=[0,0];
t0=0;
t=0:0.00001:0.001;
out=ode(y0,t0,t,f);
clf();
plot(out);

Thank you a lot


回答1:


You were nearly there, you only had problems with the shape of the vectors and how that affects the collection of the trajectory, that is, the construction of the return array of ode, as an array of vectors.

function y=u(t)
    y=(sign(t)+1)/2
endfunction

L=0.001
R=10
C=0.000001

function zdot=f(t,y)
    zdot = [ y(2); (u(t)-y(1)-L*y(2)/R)/(L*C)];
endfunction

y0=[0;0];
t0=0;
t=0:0.00001:0.001;
out=ode(y0,t0,t,f);
clf();
subplot(211)
plot(t,out(1,:),"r.--");
subplot(212)
plot(t,out(2,:),"b-..");

Note that all vectors are forced to be column vectors. And that the plotting is by components, using the provided time scale as x axis.

Also take note that the two components, function and derivative, differ significantly in their magnitudes.



来源:https://stackoverflow.com/questions/29477235/using-scilab-to-solve-and-plot-differential-equations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!