Why do I get 'Undefined function or variable'?

I'm attempting to run 'ode45' and changed the function file code to include an input vector 'u'. Now I get an error stating that 'u' is undefined. I don't know where to define 'u', since it is defined in the function?
(Undefined function or variable 'u'.)
(Error in ode45_ex>@(t,x)DOF1damped(t,x,u,w,u0,m,k,c,varargin))
the function file is:
function [dx, y] = DOF1damped(t, x, u, w, u0, m, k, c, varargin)
u = [u0*sin(w*t); u0*w*cos(w*t)];
y = [x(1);x(2)];
dx = [x(2) ;((-k/m).*x(1) - (c/m).*x(2) + (k/m).*u(1) + (c/m).*u(2))];
end
the ode file is:
clear
close all
clc
u0 = 1; %amplitude ()
w = 1; %hz
m = 0.85048569375; %kg
k = 356.9085; % N/m
c = 10; %N/m*s
tspan = [0 10];
x0 = [0;0];
[t, x] = ode45(@(t,x)DOF1damped(t, x, u, w, u0, m, k, c, varargin), tspan, x0);
data = iddata(x, t, .1);
plot(t, x);
Thanks for any help

Respuestas (1)

Stephan
Stephan el 18 de Jun. de 2019
Editada: Stephan el 18 de Jun. de 2019
If u is calculated inside the function it should not be an input argument of the function. Also delete varargin - no need - results in error, because it is a matlab function already:
u0 = 1; %amplitude ()
w = 1; %hz
m = 0.85048569375; %kg
k = 356.9085; % N/m
c = 10; %N/m*s
tspan = [0 10];
x0 = [0;0];
[t, x] = ode45(@(t,x)DOF1damped(t, x, w, u0, m, k, c), tspan, x0);
data = iddata(x, t, .1);
plot(t, x);
function [dx, y] = DOF1damped(t, x, w, u0, m, k, c)
u = [u0*sin(w*t); u0*w*cos(w*t)];
y = [x(1);x(2)];
dx = [x(2) ;((-k/m).*x(1) - (c/m).*x(2) + (k/m).*u(1) + (c/m).*u(2))];
end

5 comentarios

Thanks Stephan, this works perfectly in order to plot the data from the ode file, however it creates a new problem. These two files are used as sample data in an idnlgrey function:
filename = 'DOF1damped';
Order = [2 0 2];
u0 = 1; %amplitude ()
w = 1; %hz
m = 0.85048569375; %kg
k = 356.9085; % N/m
c = 10; %N/m*s
Parameters = {u0,w,m,k,c};
InitialStates = [0; 0];
Ts = 0;
nlgr = idnlgrey(filename, Order, Parameters, InitialStates, Ts, 'Name','DOF1grey');
nlgr = setinit(nlgr, 'Fixed', {false false});
% Estimate the initial states.
setpar(nlgr,'Fixed',{true true true true false});
opt = nlgreyestOptions('Display', 'on');
nlgr = nlgreyest(data,nlgr,'Display','Full');
c_est = nlgr.Parameters(5).Value;
[nonlinear_c_est, dnonlinear_c_est] = getpvec(nlgr,'free')
compare(data,nlgr)
I updated the order to include no input, and I received this error.
Error using iddata/nlgreyest (line 59)
The number of inputs and outputs of the model must match that of the data.
Error in DOF1grey (line 21)
nlgr = nlgreyest(data,nlgr,'Display','Full');
should not oder be:
Ordner = [1 0 2]
Ben25
Ben25 el 19 de Jun. de 2019
This doesn't work either, I believe the first number should still be 2, as it represents the y vector, which has an order of 2. I still do not know where the problem is, as there are definitely 2 outputs and 2 states.
Ben25
Ben25 el 19 de Jun. de 2019
My problem is very similar to the section 'Perform Nonlinear Grey Box Estimation' on this example https://ww2.mathworks.cn/help/ident/ug/estimating-coefficients-of-odes-to-fit-given-solution.html#d117e41137 . I don't see the problem.
Stephan
Stephan el 20 de Jun. de 2019
Maybe it helps if you attachyour data

Iniciar sesión para comentar.

Categorías

Más información sobre Programming en Centro de ayuda y File Exchange.

Preguntada:

el 18 de Jun. de 2019

Comentada:

el 20 de Jun. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by