问题
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.41327y2 = 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)
whererealf
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