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: dastew
Subject: RE: Feeding coefficients to a function file????
Date: Thu, 20 Aug 2009 20:42:38 +0000




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]