Borrar filtros
Borrar filtros

??? Undefined function or variable 'x'

1 visualización (últimos 30 días)
Richard
Richard el 28 de Mzo. de 2012
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.

Respuesta aceptada

Wayne King
Wayne King el 28 de Mzo. de 2012
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 comentarios
Wayne King
Wayne King el 28 de Mzo. de 2012
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.
Richard
Richard el 28 de Mzo. de 2012
Thanks, again, @Wayne!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Programming en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by