help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Feeding coefficients to a function file????


From: Miikka Tulonen
Subject: Re: Feeding coefficients to a function file????
Date: Fri, 21 Aug 2009 23:23:54 +0200

Ok, it seems I managed to come up with a solution to this by experimenting a little bit more. I removed ";" from ends of all lines and noticed that it prints k in a function as "[](0x0)". I tried adding additional argument ("opt") into function arguments like this 
 
function F = ode(t,C,opt,k)

and managed to get my vector k into the function file in one piece. Apparently it is required to take the "options" argument into account too in the function even if you leave it empty.

2009/8/21 Miikka Tulonen <address@hidden>
Doesn't make any practical difference.
You can say for example:
k1 = 1;
k2 = 2;
k=[k1 k2];
as a result k = [1 2];

Putting it this way:
k(1) = 1;
k(2) = 2;
leads exactly to the same outcome
k = [1 2];
(Also leads to the same error message)

I believe my problem is that at some point when I'm trying to transfer vector k into the function file when ode solver calls the file, something goes wrong. I just don't understand what, because I have no idea what goes in there as I have not yet found a way to debug it so that I could see what's going on in the function file. In matlab if you place "keyboard; pause;" above the line where the error occurs you can see all the variables loaded by the function by that point in the workspace and it's often quite easy to say what's not the way it's supposed to be....but Octave is still a total mystery to me.

It says "index exceeds matrix dimensions" which sort of indicates that k is not "undefined" (I guess) but rather a non-existent element of vector k is requested.   

2009/8/20 <address@hidden>




Date: Thu, 20 Aug 2009 22:29:18 +0200
Subject: Feeding coefficients to a function file????
From: address@hidden
To: address@hidden


I have a problem with exporting some coefficients to a function file from a script.
When I write it this way it works just fine (defining k1 & k2 in the function file itself):
Script:
    clear all; close all;
    t = [0 100];

    CA0 = 1.5;
    CB0 = 1.0;
    CC0 = 0;

    #k1 = 0.076;
    #k2 = 0.0023;

    C0 = [CA0 CB0 CC0];

    [t,C]=ode45(@ode,t,C0);

    plot(t,C)
    xlabel('time')
    ylabel('concentration')


Function:
    function F = ode(t,C)

    k1 = 0.076;
    k2 = 0.0023;

    ca = C(1);
    cb = C(2);
    cc = C(3);

    r1 = k1*ca*cb;
    r2 = k2*(cc.^2);

    dA = -r1+r2;
    dB = -r1+r2;
    dC = r1-r2;

    F = [dA; dB; dC];

    endfunction

Now, when I try to define the coefficients (k1 & k2) in the script like this..:
Script:
    clear all; close all;
    t = [0 100];

    CA0 = 1.5;
    CB0 = 1.0;
    CC0 = 0;

    k1 = 0.076;
    k2 = 0.0023;

I think these should be
      k(1)=0.076;
      k(2)=0.0023;


Doug Stewart






    C0 = [CA0 CB0 CC0];
    k =[k1 k2];

    [t,C]=ode45(@ode,t,C0,[],k);

    plot(t,C)
    xlabel('time')
    ylabel('concentration')

...and then try to pass them to the function:
Function:
1    function F = ode(t,C,k)
2
3    #k1 = 0.076;
4    #k2 = 0.0023;
5
6    ca = C(1);
7    cb = C(2);
8    cc = C(3);
9
10    k1 = k(1);
11    k2 = k(2);
12
13    r1 = k1*ca*cb;
14    r2 = k2*(cc.^2);
15
16    dA = -r1+r2;
17    dB = -r1+r2;
18    dC = r1-r2;
19
20    F = [dA; dB; dC];
21
22    endfunction

I get this:

    error: A(I): Index exceeds matrix dimension.
    error: called from:
    error:   D:\Tiedostoja\Koulu\Octave\Testi\ode.m at line 10, column 4
    error:   D:\Ohjelmat\Octave 3.2.0\share\octave\packages\odepkg-0.6.7\ode45.m at line 328, column 17
    error:   D:\Tiedostoja\Koulu\Octave\Testi\funktio.m at line 15, column 6


I figured I'm probably screwing up on the first line of the function file but couldn't find out what exactly. (I tried some other things too but at best managed only to produce a different error message, other than syntax error)
I failed to find any examples that would be doing the same thing but ode45 help indicates that I should be (I think) able to feed in
some additional parameters just like in MATLAB (I'm coming from there, started experimenting with Octave yesterday).
 
Ode45 help says:

    -- Function File: [] = ode45 (@FUN, SLOT, INIT, [OPT], [PAR1, PAR2,
             ...])
    -- Command: [SOL] = ode45 (@FUN, SLOT, INIT, [OPT], [PAR1, PAR2, ...])
    -- Command: [T, Y, [XE, YE, IE]] = ode45 (@FUN, SLOT, INIT, [OPT],
         [PAR1, PAR2, ...])
      .........

I also failed to find out a way to debug it so that I could see what's going on inside the function when it is being run. I tried "keyboard" and some other too
I think but wasn't able to take a look at any of the stuff defined in the function, only the ones in the script. When I place "keyboard;" or "keyboard;pause;" into the function file, the only things I can take a look at are the parameters in the script file.
 So it would be very helpful if someone could point out what am I doing wrong with it. Oh, and I'm using version 3.2.0 on Windows Vista.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]