Odd behavior of ODE in Scilab: equation dy/dx=A is not solved properly

て烟熏妆下的殇ゞ 提交于 2019-12-11 07:39:20

问题


I am still learning Scilab (5.5.2), so I am writing and running test codes to familiarize myself with the software.

To test the numerical differential equation solver, I started easy from the equation dy/dx = A, which has as solution y = Ax+c (line equation).

This is the code I wrote:

// Function y = A*x+1
function ydot=fn(x, A)
    ydot=A
endfunction
A=2;
//Initial conditions
x0=0;
y0=A*x0+1;
//Numerical Solution
x=[0:5];
y= ode(y0,x0,x,fn);
//Analytical solution
y2 = A*x+1;
clf(); plot(x, y); plot(x, y2, '-k');
//End

And these are the unexpected results:

y = 1. 2.7182824 7.3890581 20.085545 54.598182
148.41327

y2 = 1. 3. 5. 7. 9. 11.

It appears that y = e^x. Can someone explain what is going wrong, or what I did wrong?


回答1:


Just renaming the variables does not change how they are used internally by the ODE solver. Since that solver expects a function with arguments time,state it will interpret the provided function that way.

Renaming the variables back, what you programmed is equivalent to

function ydot=fn(t,y)
    ydot = y
endfunction

which indeed has the exponential function as solution.

From the manual you can see that the way to include parameters is to pass the function as a list,

The f argument can also be a list with the following structure: lst=list(realf,u1,u2,...un) where realf is a Scilab function with syntax: ydot = f(t,y,u1,u2,...,un)

function ydot=fn(t,y,A)
    ydot = A
endfunction

y= ode(y0,x0,x,list(fn,A));


来源:https://stackoverflow.com/questions/44705431/odd-behavior-of-ode-in-scilab-equation-dy-dx-a-is-not-solved-properly

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