Thread Subject: Passing constants to optimization functions

Subject: Passing constants to optimization functions

From: David Doria

Date: 10 Oct, 2007 15:19:17

Message: 1 of 2

I am trying to use lsqnonlin(), but my user defined function
needs to know a couple of constants. The lsqnonlin expects
my function to only accept one parameter (the one being
optimized). Is there a better way to "pass" these constants
besides making them global?

Thanks,

David

Subject: Passing constants to optimization functions

From: John D'Errico

Date: 10 Oct, 2007 15:36:26

Message: 2 of 2

"David Doria" <daviddoria@gmail.com> wrote in message <feiqhl$7nd
$1@fred.mathworks.com>...
> I am trying to use lsqnonlin(), but my user defined function
> needs to know a couple of constants. The lsqnonlin expects
> my function to only accept one parameter (the one being
> optimized). Is there a better way to "pass" these constants
> besides making them global?

Use an anonymous function.

a = 2;
f = @(x) (x-a).^2;

Obviously, the minimum occurs at x = a = 2.
Can we find it?

x = fminsearch(f,0)
x =
                         2

HTH,
John

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
optimization John D'Errico 10 Oct, 2007 11:40:23
anonymous funct... John D'Errico 10 Oct, 2007 11:40:23
rssFeed for this Thread

Contact us at files@mathworks.com