Error when running this code.

1 次查看(过去 30 天)
Westin Messer
Westin Messer 2016-12-10
评论: dpb 2016-12-10
Can anyone explain why I'm getting this error when I run this code.
assignment3_2
Undefined function or variable 'lorenz_attractor'.
Error in odearguments (line 87)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in assignment3_2 (line 2)
[t1, y1] = ode45(@lorenz_attractor,tspan,y0);
Here's the code
function [a, r2] = assignment3(XX,YY)
% linregr: linear regression curve fitting
% [a, r2] = linregr(x,y): Least squares fit of straight
% line to data by solving the normal equations
% input:
% x = independent variable
% y = dependent variable
% output:
% a = vector of slope, a(1), and intercept, a(2)
% r2 = coefficient of determination
n = length(XX);
if length(YY)~=n, error('x and y must be same length'); end
XX = XX(:); YY = YY(:); % convert to column vectors
sx = sum(XX); sy = sum(YY);
sx2 = sum(XX.*XX); sxy = sum(XX.*YY); sy2 = sum(YY.*YY);
a(1) = (n*sxy-sx*sy)/(n*sx2-sx^2);
a(2) = sy/n-a(1)*sx/n;
r2 = ((n*sxy-sx*sy)/sqrt(n*sx2-sx^2)/sqrt(n*sy2-sy^2))^2;
% create plot of data and best fit line
xp = linspace(min(XX),max(XX),2);
yp = a(1)*xp+a(2);
plot(XX,YY,xp,yp)
grid on
And this is the script I am running.
tspan = [0 30]; y0 = [5 5 5]; y00 = [5.00001 5 5];
[t1, y1] = ode45(@lorenz_attractor,tspan,y0);
[t2, y2] = ode45(@lorenz_attractor,tspan,y00);
y21 = interp1(t2,y2,t1);
Y = log(abs(y21(:,3)- y1(:,3)));
X = t1;
XX = X(266:891,1);
YY = Y(266:891,1);
lin = assignment3(XX,YY)
  3 个评论
Westin Messer
Westin Messer 2016-12-10
When I put that in my script file I still got the same error.
dpb
dpb 2016-12-10
Can't define functions in script files.
There has to be a pre-existing function of the name in an m-file on the searchable MATLABPATH that will show up when run the script.
There is a packaged demo lorenz with Matlab distribution but no builtin lorenz_attractor. I presume given the comment at the top of the code this came from some coursework code somewhere; you've not downloaded the complete code (or were to supply the function yourself, maybe???).

请先登录,再进行评论。

回答(0 个)

标签

Community Treasure Hunt

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

Start Hunting!

Translated by