[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: passing a structure to fsolve
From: |
Olaf Till |
Subject: |
Re: passing a structure to fsolve |
Date: |
Wed, 23 Mar 2011 10:29:55 +0100 |
User-agent: |
Mutt/1.5.20 (2009-06-14) |
On Tue, Mar 22, 2011 at 01:03:59PM -0700, altomare wrote:
> Hi all,
>
> I would like to pass a structure to fsolve but so far I did not have any
> luck
> Minimal example:
> %%
> clear all
>
> function r = gtest(str)
> r=str.a-str.b;
> endfunction
> test.a=2;
> test.b=3;
>
>
> If I type: gtest(test) I get
> ans = -1
> However
> [x, fval, info] = fsolve (@(var) gtest(var),test)
> returns:
> error: Invalid call to fsolve. Correct usage is: ...
> It looks to me that I cannot pass a structure to fsolve.
Yes, you can only pass a vector or matrix.
> If that is the
> case, how can I circumvent the problem?
Well, you have to pass all initial values as elements of a single
vector or matrix. fsolve will call your function (anonymous function @
(var) in your example) with a vector (or matrix, respectively)
argument. You could try to reorder the vector (or matrix) elements to
provide a structure for the call to "gtest":
... = fsolve (@ (var) gtest (<some functions> (var)), init_vals);
where <some functions> might involve "reshape", "num2cell",
"cell2struct", or even "partarray" from the miscelaneous package
(Octave Forge). Or you could put <some functions> into the body of
gtest instead. Or it might be easier to make getest directly use the
vector (or matrix).
Olaf