%----------Chua.m----------
function out = chua(t,in)
x = in(1);
y = in(2);
z = in(3);
alpha = 15.6;
beta = 28;
m0 = -1.143;
m1 = -0.714;
h = m1*x+0.5*(m0-m1)*(abs(x+1)-abs(x-1));
xdot = alpha*(y-x-h);
ydot = x - y+ z;
zdot = -beta*y;
out = [xdot ydot zdot]';
%----------StartChua.m----------
[t,y] = ode45(@chua,[0 100],[0.7 0 0]);
plot3(y(:,1),y(:,2),y(:,3))
grid
end
It confuses me, cause obviously this code was already tested. matlab tells me now i have to put an "end" for the function, even tho the original code doesnt have one, ok I did it.
Then it tells me the function is never used, and [t,y] = ode45(@chua,[0 100],[0.7 0 0]); tells me that the function chua doesnt exist ?

 채택된 답변

Star Strider
Star Strider 2021년 7월 17일

1 개 추천

In a script, all the function definitions must go at the end of the script:
%----------StartChua.m----------
[t,y] = ode45(@chua,[0 100],[0.7 0 0]);
plot3(y(:,1),y(:,2),y(:,3))
grid
%----------Chua.m----------
function out = chua(t,in)
x = in(1);
y = in(2);
z = in(3);
alpha = 15.6;
beta = 28;
m0 = -1.143;
m1 = -0.714;
h = m1*x+0.5*(m0-m1)*(abs(x+1)-abs(x-1));
xdot = alpha*(y-x-h);
ydot = x - y+ z;
zdot = -beta*y;
out = [xdot ydot zdot]';
end
That seems to work.
See Create Functions in Files for details.
.

댓글 수: 6

Martin Mayer
Martin Mayer 2021년 7월 17일
Thank you for your answer.
I still get the error:
Unrecognized function or variable 'chua'.
Error in odearguments (line 90)
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);
when I use this in command window:
[t,y] = ode45(@chua,[0 100],[0.7 0 0]);
plot3(y(:,1),y(:,2),y(:,3))
grid
Star Strider
Star Strider 2021년 7월 17일
My pleasure!
It runs as posted in R2021a. It may be necessary to save the ‘chua’ function as ‘chua.m’ on your MATLAB search path with a version/release that does not support functions in files, specifically R2016a and earlier.
.
Martin Mayer
Martin Mayer 2021년 7월 17일
I have R2020b. I tried other code before that used functions, it worked, but this one doesn't.
If function has same name as the script, it says Error: File: chua.m Line: 11 Column: 16
Local function name must be different from the script name.
Star Strider
Star Strider 2021년 7월 17일
Since you have R2020b, if you copy and save the code I posted as a separate script (save it as whatever name you want), it should work just as it worked here.
I have no idea why it doesn’t work for you.
Also, try running the file I just attached. It ran without error when I tried it, and produced the desired result.
.
Martin Mayer
Martin Mayer 2021년 7월 17일
Your file worked instant. I dont get it, but thanks. :)
Star Strider
Star Strider 2021년 7월 17일
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

질문:

2021년 7월 17일

댓글:

2021년 7월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by