Using ode45 in matlab problem!

  • Thread starter Hymne
  • Start date
  • #1
89
0

Homework Statement


Hello! I have been given a problem of ordinary differential equations to be solved in matlab by ode45. The equations are on a sheet but you can see what equations i put in. I did as the teacher instructed but matlab gives me a error message. Can you see what I misunderstood? :/
'prim' is the vector of derivatives so I think I have written the problem in the right structure.

The Attempt at a Solution


First m-file, the function file (name blandning.m):

PHP:
function prim = blandning(tid, x1, x2, x3, s, q, w);
s = 1;
q = 1;
w = 0.1610; %Parameters

prim = [s*(x2 - x2*x1 + x1 - q*x1^2); -s^(-1)*(x2 - x1*x2 + x3); w*(x1 - x3)];
end
The main file (name huvud.m):

PHP:
clc
clear all

s = 1;
q = 1;
w = 0.1610;                 %Parameters

x1 = 30; 
x2 = 1; 
x3 = 30;                    %intialconditions
tf = 10;                    %final time

x0 = [x1; x2; x3];          %intial vector

tid = [0, tf];
[t, x] = ode45(blandning, t, x0, [], s, q, w);

plot(t, x)
The error message, when trying to run huvud.m says:

"??? Input argument "x2" is undefined.

Error in ==> blandning at 7
prim = [s*(x2 - x2*x1 + x1 - q*x1^2); -s^(-1)*(x2 - x1*x2 + x3); w*(x1 - x3)];

Error in ==> Huvud at 17
[t, x] = ode45(blandning, t, x0, [], s, q, w);
"


Thanks for all help possible!

/ Hymne
 

Answers and Replies

  • #2
NascentOxygen
Staff Emeritus
Science Advisor
9,244
1,072
Hi Hymne! I know nothing about Matlab. But I can see some things which are unusual so if I point them out you can either defend them or fix them.
Code:
tid = [0, tf];
[t, x] = ode45(blandning, t, x0, [], s, q, w);
➳ You define tid but don't pass it in any argument list. This probably indicates a mistake!
➳ You have t in the argument list but t has not been initialised anywhere before it is used. You have t on both the left and right sides, so I suspect t probably should not be in the argument list at all!
➳ You have an empty set in the argument list, check that that is correct. I suspect that it is this non-value that blandning is trying to use as its value for x2 of which it complains.
➳ Your function prim seems strangely written, it appears to assign to prim in two places, but as I said I know nothing about Matlab so really can't say whether this is okay or not.

I think you are going to have to spend more time exploring and learning about Matlab before you make much progress with this. You can't hope to devise correct code without a good understanding of what you are doing. https://www.physicsforums.com/images/icons/icon9.gif [Broken]
 
Last edited by a moderator:
  • #3
319
0
Your function is not defined properly. The built in function ode45 can integrate a coupled set of equations, but if you want to do it that way, it all has to be part of one array of variables. So you want to pass your "blandning" function a single vector x (which will be a 3x1 array), and then use indexing to evaluate the RHS of the ODE (e.g. the third element will be w*(x(1) - x(3)) ). So your function definition should be changed to the following if you want to use it with ode45:

function prim = blandning(t, x, s, q, w)

Now when calling this function with ode45, you want to give it the initial condition you established, so the correct syntax is

[t,x] = ode45(blandning, tid, x0)

because the first argument is always the name of the function, the second is the time span, and the third is the initial condition.

Now, it's a little trickier (but quite possible) to include the constant coefficients as parameters to pass to the function. There are several ways to do this, but I suggest taking the easy route and just deleting them from the blandning argument list; if they're already defined inside the body of the function, you don't need to pass those numbers to the function every time anyway.

(Note to Nascent: variables don't need to be initialized in MATLAB, so it's ok to pass an undefined variable to a function. MATLAB will assign to it the type needed to fill whatever function it serves. Also, the syntax for function definitions is being correctly applied: the "prim" on top lets MATLAB know that the name of the function is blandning, and that the output will be prim. So, the program knows to look inside the function and find prim when this function is called for output.)
 

Related Threads on Using ode45 in matlab problem!

  • Last Post
Replies
5
Views
2K
  • Last Post
Replies
3
Views
2K
Replies
1
Views
887
Replies
1
Views
2K
  • Last Post
Replies
1
Views
8K
  • Last Post
Replies
2
Views
2K
Replies
0
Views
3K
  • Last Post
Replies
3
Views
2K
  • Last Post
Replies
8
Views
6K
Replies
1
Views
3K
Top