"? Input argument "x2" is undefined. MATLAB

  • Thread starter Thread starter Hymne
  • Start date Start date
  • Tags Tags
    Matlab Ode45
Click For Summary
SUMMARY

The forum discussion addresses an error encountered in MATLAB while solving ordinary differential equations using the ode45 function. The user, Hymne, receives feedback indicating that the function 'blandning' is incorrectly defined, specifically regarding the input arguments. The suggested correction involves changing the function definition to accept a single vector for state variables and adjusting the ode45 call to use the correct time vector. The final recommendation is to simplify the function by removing unnecessary parameters that are already defined within the function body.

PREREQUISITES
  • Understanding of MATLAB syntax and function definitions
  • Familiarity with the ode45 function for solving differential equations
  • Knowledge of vector and matrix operations in MATLAB
  • Basic concepts of ordinary differential equations (ODEs)
NEXT STEPS
  • Learn how to define functions in MATLAB with multiple input arguments
  • Research the correct usage of the ode45 function, including its input parameters
  • Explore MATLAB's vectorization techniques for efficient computation
  • Study examples of solving ODEs in MATLAB to reinforce understanding
USEFUL FOR

Students and professionals working with MATLAB for numerical analysis, particularly those solving ordinary differential equations and seeking to improve their coding practices in MATLAB.

Hymne
Messages
87
Reaction score
1

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
 
Physics news on Phys.org
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
 
Last edited by a moderator:
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 let's 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.)
 

Similar threads

  • · Replies 6 ·
Replies
6
Views
5K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 16 ·
Replies
16
Views
4K
  • · Replies 25 ·
Replies
25
Views
5K
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K