1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Using ode45 in matlab problem!

  1. Apr 27, 2012 #1
    1. The problem statement, all variables and given/known data
    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.

    3. 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
     
  2. jcsd
  3. Apr 27, 2012 #2

    NascentOxygen

    User Avatar

    Staff: Mentor

    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 (Text):

    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: May 5, 2017
  4. Apr 27, 2012 #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 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.)
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Using ode45 in matlab problem!
  1. Matlab: ode45 help (Replies: 1)

  2. Matlab ode45 Problem (Replies: 5)

Loading...