Difficulties solving ODE in Octave

Click For Summary

Discussion Overview

The discussion revolves around difficulties encountered while solving ordinary differential equations (ODEs) using Octave, specifically focusing on the implementation of a function and its execution within the software. Participants explore issues related to function arguments and script file naming conventions.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant describes successfully running a piece of code in the Command window but encountering an error when trying to execute the same code as a saved function in a script file.
  • Another participant points out that the function requires two arguments, x and t, which must be defined before calling the function from the Command window.
  • A participant expresses confusion about where x and t come from when the code runs successfully in the Command window but fails when saved as a function.
  • Suggestions are made to modify the function to define x and t within the script or to call the function with defined arguments from the Command window.
  • Concerns are raised about naming the script file the same as the function defined within it, which could lead to conflicts.
  • A participant shares a helpful resource on writing script files for Octave, which aids in resolving their issue.

Areas of Agreement / Disagreement

Participants generally agree on the need to define function arguments when calling a function, but there is some disagreement regarding the best practices for naming scripts and functions to avoid conflicts.

Contextual Notes

Some participants mention specific requirements for defining variables and function calls, but the discussion does not resolve all uncertainties regarding the best approach to structuring the code.

Who May Find This Useful

This discussion may be useful for users new to Octave, particularly those interested in solving ODEs and understanding the nuances of function definitions and script file management.

Dr.D
Messages
2,411
Reaction score
723
TL;DR
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   Reactions: Wrichik Basu and jedishrfu

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
3
Views
2K
Replies
1
Views
2K
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 7 ·
Replies
7
Views
4K