Difficulties solving ODE in Octave

In summary, you need to supply x and t from the command window in order to run the pend function. If you want to define x and t in the script itself, you can do so by changing your function: function xdot = pend() x = ... %define x t = ... %define t %Now do your work with x and t endfunction. Alternately, you can run the script runplot.m and execute it that way.
  • #1
Dr.D
2,412
722
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
  • #2
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.
 
  • #3
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?
 
  • #4
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)
 
  • #5
Your script shouldn't be called pend since that's the name of a function you've defined within it.

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,"-@")

so perhaps you could call your script runplot.m and execute it that way.
 
  • #7
@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

FAQ: Difficulties solving ODE in Octave

1. What is an ODE?

An ODE, or ordinary differential equation, is a mathematical equation that describes the relationship between a function and its derivatives. It is commonly used to model changes in a system over time.

2. How is Octave used to solve ODEs?

Octave is a programming language and software environment that can be used to solve ODEs numerically. It has built-in functions for solving initial value problems and boundary value problems.

3. What are some common difficulties when solving ODEs in Octave?

Some common difficulties include choosing an appropriate solver, ensuring stability and accuracy of the solution, and handling stiff equations. Additionally, errors can occur if the initial conditions are not properly specified or if the ODE is not correctly defined.

4. How can I troubleshoot errors when solving ODEs in Octave?

If you encounter errors when solving ODEs in Octave, you can try checking your code for typos or errors in the equation definition. You can also try using a different solver or adjusting the parameters of the solver. Additionally, you can consult online resources or seek help from other Octave users.

5. Are there any tips for improving efficiency when solving ODEs in Octave?

There are a few ways to improve efficiency when solving ODEs in Octave. One is to use vectorization, which involves performing operations on arrays instead of individual elements. Another is to try different solvers and adjust their parameters for better performance. Additionally, you can try optimizing your code by minimizing unnecessary calculations or using built-in functions instead of custom ones.

Similar threads

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