问题描述
如何使用scilab ode()函数求解二阶微分方程。
(例如:y''+ 3y'+ 2y = f(x),y(0)= 0,y'(0)= 0)
然后绘制函数y( x)。
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).
我想用它来模拟带有阶跃函数输入的RLC电路信号
I want to use this to model the RLC-circuit signal with step-function input
这是我尝试的代码
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);
非常感谢
推荐答案
您就在那儿,矢量形状的问题以及它如何影响轨迹的集合,也就是 ode的返回数组的构造,您都只有问题
,作为向量数组。
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-..");
请注意,所有向量都被强制为列向量。并且使用提供的时间刻度作为x轴按组件进行绘制。
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.
这篇关于使用scilab求解和绘制微分方程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!