??? Undefined function or variable 'x'
3 ビュー (過去 30 日間)
古いコメントを表示
I am trying to use ode45 to solve a system of ODEs.
What I did was:
function Testfunction
tspan = [...]
%specify initial conditions
x0 = [...,...,...,...]
[t,x] = ode45(@somefn, tspan, x0)
%defining the output variables
y=x(:,1);
etc...
%plot stuff
plot(...)
return
%now define somefn
function g = somefn(t,x)
g = zeros(size(x));
g(1) = y;
etc...
return
But when I tried to run this code MATLAB keeps saying ??? Undefined function or variable 'x' in somefn
please could someone help me? Thanks.
0 件のコメント
採用された回答
Wayne King
2012 年 3 月 28 日
Hi Richard, save your function
function g = somefn(t,x)
g = zeros(size(x));
g(1) = y;
end
in some folder on the MATLAB path and then call
[t,x] = ode45(@somefn,tspan,x0);
at the command line.
You are going to have a problem though, because somefn.m has to know what y is. It looks like you are using ode45 to return x and then using x to compute y, but you never pass y to the function somefn.m.
You have to give somefn.m everything it needs. Perhaps you can define that y internally inside the function?? Although you appear to have some recursive situation going on where you need the output of ode45() to feedback into ode45()...
3 件のコメント
Wayne King
2012 年 3 月 28 日
If you look at the template he gives you for the integrating function, everything that is computed in somefn.m (your name) is something that is either based a variable passed to the function, or something declared inside the function. Once ode45() returns x, then you can do things like
y = x(:,1);
you have inside of somefn.m
g(1) = y;
but somefn.m has no idea what y is. You do not pass y to somefn.m as an input.
その他の回答 (0 件)
参考
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!