"? Input argument "x2" is undefined. MATLAB

In summary, the student has tried to solve ordinary differential equations using MATLAB and has run into difficulties. Prim is undefined and the student is missing a step in the function.
  • #1
Hymne
89
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
  • #2
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:
  • #3
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.)
 

1. What is ode45 in MATLAB?

ODE45 is a function in MATLAB that solves ordinary differential equations (ODEs) using a medium-order, medium-strength explicit Runge-Kutta method. It is a built-in function and can be accessed by typing "ode45" in the command window.

2. How do I use ode45 in MATLAB?

To use ode45, you need to provide the function representing the ODE, initial conditions, and the time span for which you want to solve the ODE. The syntax for using ode45 is: [t,y] = ode45(@odefun, tspan, y0), where "@odefun" is the name of the function representing the ODE, "tspan" is the time span, and "y0" is the initial condition.

3. What are the advantages of using ode45 in MATLAB?

ODE45 is a popular and efficient method for solving ODEs in MATLAB. It is a medium-order, medium-strength method, which means it provides a good balance between accuracy and computation time. It also handles stiff ODEs well, and the user has control over the error tolerances and the step size. Additionally, it has a built-in adaptive step size control, which helps in solving ODEs with varying stiffness.

4. What are the limitations of ode45 in MATLAB?

While ode45 is a robust and efficient ODE solver, it may not be the best choice for all types of ODEs. It may not perform well for very stiff ODEs or ODEs with discontinuities. In such cases, other methods like ode23 or ode15s may be more suitable. Additionally, ode45 may not always provide the most accurate solution, and the user should carefully choose the error tolerances based on their specific problem.

5. Can ode45 be used for systems of ODEs?

Yes, ode45 can be used to solve systems of ODEs. The function representing the ODEs should have a vector input and output, with the vector representing the state variables of the system. The initial condition should also be a vector with the same size as the state variables. For example, if the system has two state variables, the initial condition should be a vector of size 2. The output of ode45 will also be a matrix with the first column representing the time and the remaining columns representing the state variables at each time step.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
6
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
992
  • Engineering and Comp Sci Homework Help
Replies
1
Views
875
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Introductory Physics Homework Help
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • Calculus and Beyond Homework Help
Replies
9
Views
5K
  • Programming and Computer Science
Replies
6
Views
1K
Back
Top