Reputation: 13
I got a problem with running Octave function (ODE), I've tried already present solutions for this problem but nothing is working. I've also tried by saving my filename as egzamin.m
but it too not worked.
Code from octave :
function dx=egzamin(x,t)
dx=zeros(4,1);
b=0;
g=9.81;
x1=x(1);
y1=x(2);
Vx=x(3);
Vy=x(4);
dx(1)=Vx;
dx(2)=Vy;
dx(3)=-b*Vx*sqrt(Vx.^2+Vy.^2);
dx(4)=-b*Vy*sqrt(Vx.^2+Vy.^2)-g;
endfunction
N=mod(291813,100);
x1=0;
y1=0;
Vx=20+N;
Vy=20+N;
t=0:0.01:500;
sol=lsode("egzamin",[x1,y1,Vx,Vy],t);
plot(sol(:,1),sol(:,2))
The error is :
error: 'x' undefined near line 5 column 4
error: called from
egzamin at line 5 column 3
Upvotes: 1
Views: 13183
Reputation: 429
Since this caused me more trouble than it was worth, I'll mention a few things to check. Firstly, octave may be abbreviating the proper error message which in matlab would be:
Output argument "X" (and possibly others) not assigned a value in the execution with "my_function" function.
Which means that you should make sure all paths assign something to X
as defined in the function as:
function X=my_function
end
If you are writing a script file, make sure the scripting and function calling occurs before the function definitions, which is opposite to most traditional programming languages which usually require at least the declarations to come before the calling code.
Also if you are using the answer here about specifying a dummy value, it is probably better to use:
% dummy statement to get a script file instead of a function file
1;
It might work in octave, but the # comment notation didn't compile in matlab.
Upvotes: 0
Reputation: 2509
Since the file starts with function
, it is not a script file,
as explained in the doc:
Unlike a function file, a script file must not begin with the keyword function
Add any statement (even dummy like 1;
) before the function
line to get a script file.
# dummy statement to get a script file instead of a function file
1;
function dx=egzamin(x,t)
g = 9.81;
Vx = x(3);
Vy = x(4);
dx = [Vx, Vy, 0, -g];
endfunction
N=mod(291813,100);
x1=0;
y1=0;
Vx=20+N;
Vy=20+N;
t=0:0.01:500;
sol=lsode("egzamin",[x1,y1,Vx,Vy],t);
plot(sol(:,1),sol(:,2))
A very clear explanation of what's going on is given here.
Upvotes: 2
Reputation: 18187
You need to save the function (thus from function
to endfunction
and naught else) as egzamin.m, and then execute the rest of the code in a script or at the command line. Alternatively, provided Octave does that the same as what MATLAB does nowadays, first put your script (N=(..)
to plot()
) and then the function.
This is necessary since you are defining your function first, so it doesn't have any inputs yet, as you don't define them until later. The function needs to have its inputs defined before it executes, hence you need to save your function separately.
You can of course save your "script" bit, thus everything which is currently below your function declaration, as a function as well, simply don't give it in- and outputs, or, set all the input parameters here as well. (Which I wouldn't do as it's the same as your egzamin then.) e.g.
function []=MyFunc()
N=mod(291813,100);
x1=0;
y1=0;
Vx=20+N;
Vy=20+N;
t=0:0.01:500;
sol=lsode("egzamin",[x1,y1,Vx,Vy],t);
plot(sol(:,1),sol(:,2))
endfunction
Upvotes: 0