Using scilab to solve and plot differential equati

2019-09-03 11:19发布

问题:

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.