0

I'm trying to solve the following ODE:

where R(T) is defined as:

This is my not so great attempt at using Octave:

1;

function xdot = f(t, T)
xdot = 987 * ( 0.0000696 * ( 1 + 0.0038 * ( T(t) - 25 ))) - ( 0.0168 * (T(t)-25 )) - (( 3.25 * 10 ^ (-13))) * ((T(t))^4 - (25^4));
endfunction

[x, istate, msg] = lsode( "f", 100, (t=linspace(0,3600,1000)'));

T_ref and T_infinity_sign are the same constant. Why isn't my code correct?

Adriaan
  • 17,741
  • 7
  • 42
  • 75
  • It is just T, not T(t). Octave is a little special with function handles, so instead of "f" (which tries to read a file f.m?) use @(t,T)f(t,T). In Matlab this is shortened to just @f. – Lutz Lehmann Jun 13 '22 at 20:05
  • 2
    read the documentation of lsode (`help lsode` in the octave terminal). Your use of "f" seems fine. Not sure if your function is defined on the commandline or in its own file. Perhaps it would be better to have it in its own file. But in any case, it looks to me as if your arguments are reversed? In the documentation it implies the first argument should be a vector, and the second is a scalar (presumably indicating time). – Tasos Papastylianou Jun 13 '22 at 22:36
  • @LutzLehmann, so when use "f" in this way, it's looking for a file called f.m? Your suggestion is to replace it with @(t,T)f(t,T)? Could you explain you suggestion? – RogerDodger Jun 15 '22 at 15:02
  • @TasosPapastylianou my understanding is that the way "f" is defined here is how I used - function xdot = f(t,T), is that not the case? oh okay I'll look into the order of my arguments more. Do you know what the error message is telling me? – RogerDodger Jun 15 '22 at 15:04
  • I'm not completely sure how that string gets interpreted, it could work as it is in this detail. Constructing a function handle my way works with ode45 and similar, it is some time that I used lsode. // What octave version are you using? – Lutz Lehmann Jun 15 '22 at 15:10
  • Re your original post: please [edit] your post to add code and data as text ([using code formatting](https://stackoverflow.com/editing-help#code)), not images. Images: A) don't allow us to copy-&-paste the code/errors/data for testing; B) don't permit searching based on the code/error/data contents; and [many more reasons](https://meta.stackoverflow.com/a/285557). Images should only be used, in addition to text in code format, if having the image adds something significant that is not conveyed by just the text code/error/data. See [mcve] on what code is required. – Adriaan Jun 16 '22 at 09:22

1 Answers1

0

If you type

debug_on_error(1)

on your octave session, and then run your code, you will see that the "f" part is called as expected, but then it fails inside lsode with the following error:

error: T(100): out of bound 1 (dimensions are 1x1)
error: called from
    f at line 4 column 8

If you look at the documentation of lsode, it says it expects a function f whose first argument is a state vector x, and the second is a scalar, corresponding to time t at which that state vector occurs; f is expected to output the differential dx/dt at time t for state vector x.

From your code it seems that you are reversing the order of the arguments (and their meanings).

Therefore, when you passed T as a second argument to your function, lsode treats it like a scalar, so when you then try to evaluate T(t), it fails with an 'out_of_bounds_ error.

My advice would be, read the documentation of lsode and have a look at its examples carefully, and start playing with it to understand how it works.


PS. The debug_on_error directive launches the debugger if an error occurs during code execution. To exit the debugger type dbquit (or if you're using the GUI, click on the 'Quit Debugging Mode' button at the top right of the octave editor). If you don't know how to use the octave debugger, I recommend you spend some time to learn it, it is a very useful tool.

Tasos Papastylianou
  • 21,371
  • 2
  • 28
  • 57