Difficulties solving ODE in Octave

AI Thread Summary
The user is struggling to solve ordinary differential equations (ODEs) in Octave, specifically when transitioning from running code directly in the Command window to saving it as a function in a file. The error arises because the function 'pend' requires two arguments, x and t, which must be defined before calling the function. To resolve this, users can either define x and t within the function or supply them as arguments when calling the function from the Command window. Additionally, renaming the script file to avoid conflicts with the function name and ensuring proper argument definition allows the code to run successfully. The user successfully resolves the issue with guidance from others in the forum.
Dr.D
Messages
2,411
Reaction score
723
TL;DR Summary
A short bit of Octave code that works fine in the Command window produces errors when saved and then executed. What am I doing wrong?
I'm new to using Octave 5.1.0, and a bit confused about how to solve ODEs with Octave. Let me show you a bit of code that I grabbed off a university web site:

>> function xdot = pend(x,t) % pend.m
xdot(1) = x(2); xdot(2) = - x(1) - 0.1*x(2);
end
>> sol=lsode( "pend",[0.1, 0.2], t = linspace(0,40, 200));
>> plot( t, sol,"-@")

If I past that code into the Command window, it runs beautifully and produces the plot that would be expected.

Now, let me save that exact same bit of code as pend.m. If I go to the Command window and type "pend", I get an error message:
error: 'x' undefined near line 2 column 13
error: called from
pend at line 2 column 11

If I go to the the Editor window and load the file pend.m, when I then try to save and run it, I get the same error message as above. What am I missing here?

Help will be greatly appreciated.

PS: If it is relevant, I'm running Win10 64 bit on an HP Z210 machine.
 
Physics news on Phys.org
Dr.D said:
Now, let me save that exact same bit of code as pend.m. If I go to the Command window and type "pend", I get an error message:
error: 'x' undefined near line 2 column 13
error: called from
pend at line 2 column 11
Your function pend has two arguments: x and t. Have you supplied the arguments from the command window? You have to call the function as >>pend(x, t) where you have to supply x and t, i.e. x and t should be previously defined in your memory from command window. It is just like a function call in other languages like Java.
 
Actually, I've not done anything different from my first post, so the short answer is, no, I have not supplied x and t. (I really would not know how to do that at this point.)

Even so, when I past that bit of code into the command window, it runs fine. Where do x and t come from in that case?

More to the point, what do I need to do so that I am able to save the file (for future modifications/documentation) and still have the computation run?
 
pend is a function with two arguments. It is like a mathematical function ##z = f(x, y)##. If you do not supply x and y to f, can you expect to get a numerical value for z? Same is the case here.

If you want to define x and t in the script itself, you can do so by changing your function:
Matlab:
function xdot = pend()
    x = ... %define x
    t = ... %define t
    %Now do your work with x and t
endfunction
You can run this from the command window as >>pend.

If you do not wish to modify the current function, do the following from the command window:
Matlab:
>>x = ... %define x
>>t = ... %define t
>>pend(x, t)
 
Your script shouldn't be called pend since that's the name of a function you've defined within it.

[CODE lang="matlab" title="sample octave code"]% DEFINING a function called pend(x,t)

>> function xdot = pend(x,t) % pend.m
xdot(1) = x(2); xdot(2) = - x(1) - 0.1*x(2);
end

% USING lsode to run the pend function with x=[0.1 0.2]
% and t=linspace(0,40, 200) ie t=[0, 40, 80, 120, 160, 200]
%
% and while pend(x,t) doesn't use t to compute anything
% function lsode(...) repeatedly calls it for each element of the t array
%
>> sol=lsode( "pend",[0.1, 0.2], t = linspace(0,40, 200));
>> plot( t, sol,"-@")[/CODE]

so perhaps you could call your script runplot.m and execute it that way.
 
@jedishrfu: I found the missing link in the GNU Octave: Script Files link that you sent; thank you. It was as simple as changing file name and putting some other code (>> 1;) above the function definition. Now it all works fine.

Many thanks to both jesishrf and Wrichik Basu for their help.
 
  • Like
Likes Wrichik Basu and jedishrfu

Similar threads

Replies
4
Views
2K
Replies
1
Views
2K
Replies
5
Views
2K
Replies
4
Views
1K
Replies
4
Views
3K
Replies
4
Views
2K
Replies
7
Views
4K
Back
Top