help-octave
[Top][All Lists]
Advanced

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

Re: defining a function from 2 other ones


From: James Sherman Jr.
Subject: Re: defining a function from 2 other ones
Date: Thu, 16 Aug 2007 13:42:49 -0400

While it would be cool to be able to define + in such a way as to take two functions f and g, and have it output a new function h in such an intuitive manner, I'm pretty sure that it can't be done in octave.  (Actually, I'm not sure what languages this would be possible, Lisp maybe?  I'm not sure how you would go about dynamically defining a function).

Anyway, back on track, the function leasqr requires a function F of the form F(x, p) or in octave terms
F = @(x, p) <stuff involving x, p>

(I'm currently having a case of deja vu here, haven't we discussed this before?)
So if you wanted to add your two functions f and g, first you need to define k (or is k a parameter you want to optimize?).  If k is a constant you can simply write
k = 10;
f=@(x,p)  p'*x
g=@(x) x.^k
h=@(x,p) f(x,p) + g(x)

And this would satisfy your the requirement for leasqr.  On the other hand if you want k to be a parameter you want optimized as well, you'll need to incorporate it into your "p" variable, so the "new" p = [oldp; k];
Then you would get something like:

f=@(x,p)  p(1:end-1)'*x
g=@(x,p) x.^p(end)
h=@(x,p) f(x, p) + g(x, p)

or equally good (or maybe even better)

f=@(x,p)  p'*x
g=@(x,k) x.^k
h=@(x,p) f(x, p(1:end-1)) + g(x, p(end))

Hope this helps.

James

On 8/16/07, address@hidden < address@hidden> wrote:
Hi,
I want to summe 2 functions and create a 3dr one.
Is it possible?

Example:
f=@(x,p)  p*x
g=@(x,k) x.^k
h=@(x,p,k) f + g

h must be a defined function that i can input as curve
mocel in optimization functions like leasqr.
Thanks in advance for any tip ...
oxy :-)
_______________________________________________
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]