help-octave
[Top][All Lists]
Advanced

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

Re: independent variable in function for leasqr


From: James Sherman Jr.
Subject: Re: independent variable in function for leasqr
Date: Mon, 9 Jul 2007 11:28:02 -0400

Hello,

Ah, I forgot to emphasize that in my code, the line
k=2;
precedes
F = inline("  p(1)*exp(-p(2)*x.^2*k)   ", "x", "p")
This is because when you call the inline function, it looks for the constants when you call the function.  For example, if I do:
k = 2;
F = inline("k*x^2", "x");
then,
F(2) = 8;
Now even if I change k after this, the function F will be unchanged.

Furthermore for leasqr, when you define F like this:
F = inline("Ao*exp(-D*x^2*k)","Ao","D","x")
You still have the problem that leasqr expects a function of the form: f(x,p)
This implies two important things:
1) the function has exactly 2 input arguments
2) the x variable is the first argument.
This is the reason that I said to let p(1) be your Ao and p(2) be your D, so that you can fit both the parameters that you want to optimize into one input variable (namely p).
For leasqr your function (if it is inline) should always look like this:
F = inline("<some function with x and p>", "x", "p");

(Note: Notice this is not the same as writing F = inline("<some function>", "p", "x");  The order you list the parameters after the function is the order that octave will expect them when you call F, so "x" must be first for leasqr)

This is also how you tell octave which variable is the independent one (it is always the first argument of F) and the parameters to be optimized (always the second argument of F).
So after you put it into this form and run leasqr, you can get the estimates of Ao and D by doing:
estimateAo = p1(1);
estimateD = p1(2);

Hope this helps.


On 7/9/07, address@hidden <address@hidden> wrote:
Hi James,
thanks for the help. Unfortunately this code does not work for me (details at
the end). I cannot make a function work if not all letter constants are
specified as variables. Example: this works
F=inline("Ao*exp(-D*x.^2*k)","Ao","D","x","k"); F(1,2,3,4)

But only if Ao, D, x and k are specified as variables, even if i want
Ao, D, and k to be constants. So this wont work
F=inline("Ao*exp(-D*x.^2*k)","Ao","D","x"); k=1; F(1,2,3)

As a results, i do not know how to tell leasqr which is the
independent variable and which constants are to
be optimized when i type leasqr(g,A, guess, F ).
How to give this information to leasqr?

Following your last email I had the following result:
octave-2.1.73:116> g=(1:2:10)'; A=exp(g); Ao=1; k=2; guess=[1 0.1]';
octave-2.1.73:118> F = inline("Ao*exp(-D*x^2*k)","Ao","D","x")
octave-2.1.73:121> leasqr(g,A, guess, F )
error: `x' undefined near line 117 column 23
error: evaluating binary operator `^' near line 117, column 24

Is it maybe the octave version?
Thanks again for any help ...

> Lets let p(1) = Ao and p(2) = D.
> Now our function is defined this way:
> k=2;
> F = inline("  p(1)*exp(-p(2)*x.^2*k)   ", "x", "p");
>
> The "x" and the "p" denote that they are the only input variables.  Notice
> also the .^ there because when leasqr calls F it will put in the whole
> vector x (in this case your variable g) and expects the output of F to be a
> vector the same size as y (or in your case A).  So, my code looks like:
>
> g=(1:2:10)';
> A=exp(g);
> k=1;
> F = inline("  p(1)*exp(-p(2)*x.^2*k)   ", "x", "p");
> guess=[1 0.1]';
> [f1, p1, kvg1, iter1, corp1, covp1, covr1, stdresid1, Z1, r21] = leasqr(g,A,
> guess, F );
>
_______________________________________________
Help-octave mailing list
address@hidden
https://www.cae.wisc.edu/mailman/listinfo/help-octave


reply via email to

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