|
From: | Michael Teitelbaum |
Subject: | RE: fminunc and for loop |
Date: | Tue, 10 Jul 2012 14:32:42 -0400 |
Thanks for the quick responses Juan and Jordi. I tried following both of your instructions, but ran into some difficulties with each: Juanian Method: I think I'm close, but don't have the syntax exactly right. I tried:
function v = my_func(x,A) > v = 0 > for i = 5:35 > v += ((((A(i,3) / 1000 + x(1) + A(i,4) / 1000 - ((A(i,3) / 1000 + x(1) + A(i,4) / 1000) ^ 2 - 4 * A(i,3) / 1000 * A(i,4) / 1000) ^ 0.5) / 2 * 1.4e-3 - (1.4e-3 - 8e-6) * (A(i-1,3) / 1000 + x(1) + A(i-1,4) / 1000 - ((A(i-1,3) / 1000 + x(1) + A(i-1,4) / 1000) ^ 2 - 4 * A(i-1,3) / 1000 * A(i-1,4) / 1000) ^ 0.5) / 2) * x(2)) * 1e6 - A(i,1)) ^ 2 > endfor > endfunction A = dlmread("/users/michael/desktop/MN4cp2Quin.CSV", ",") fminunc(@(x)my_func(x,A),[0,0]) What I want as my output is the values of x(1) and x(2) that minimize the function, but what I got was around 100 lines of : v = 0 v = 1515.1 v = 3311.6 v = 4734.4 and so on. Jordian method: I received the following error when I inputted your code: error: `x' undefined near line 2 column 29 error: called from: error: midterm at line 2, column 5 error: evaluating argument list element number 1 error: f at line 5, column 5 error: /sw/share/octave/3.6.1/m/optimization/fminunc.m at line 149, column 6 Was your code meant to be put directly into octave, or was it kind of just a general idea of the type of thing I should try? (Either way, thanks so much for the thought and effort you put into your response) If it's the latter, I won't bother trying to pursue this because I don't know enough about programming to write something similar to what you sent me. Thanks again guys, Michael > Date: Tue, 10 Jul 2012 19:34:59 +0200 > Subject: Re: fminunc and for loop > From: address@hidden > To: address@hidden > CC: address@hidden > > On Tue, Jul 10, 2012 at 7:23 PM, Michael Teitelbaum > <address@hidden> wrote: > > Hi, > > I am trying to perform the fminunc function, but I want to use a for loop > > within my function, and I'm not sure exactly how to do that. > > What I want is: > > > > fminunc(f,[0,0]) > > where f equals the sum of: > > > > f = @(x)((((A(i,3) / 1000 + x(1) + A(i,4) / 1000 - ((A(i,3) / 1000 + x(1) + > > A(i,4) / 1000) ^ 2 - 4 * A(i,3) / 1000 * A(i,4) / 1000) ^ 0.5) / 2 * 1.4e-3 > > - (1.4e-3 - 8e-6) * (A(i-1,3) / 1000 + x(1) + A(i-1,4) / 1000 - ((A(i-1,3) / > > 1000 + x(1) + A(i-1,4) / 1000) ^ 2 - 4 * A(i-1,3) / 1000 * A(i-1,4) / 1000) > > ^ 0.5) / 2) * x(2)) * 1e6 - A(i,1)) ^ 2 > > > > for i between 5 and 35. > > > > If it is not possible to use a for loop within fminunc, is there a good way > > to get octave to write out that function for me instead of me having to > > write it out manually? > > > > Any help would be greatly appreciated. > > Thanks, > > Michael > > > > _______________________________________________ > > Help-octave mailing list > > address@hidden > > https://mailman.cae.wisc.edu/listinfo/help-octave > > > > You can create a function (not a anonymous one) > > function v = my_func(x,A) > %% Do your magic here > endfunction > > and then define A and call > fminunc(@(x)my_func(x,A),[0,0]) > > -- > M. Sc. Juan Pablo Carbajal > ----- > PhD Student > University of Zürich > http://ailab.ifi.uzh.ch/carbajal/ |
[Prev in Thread] | Current Thread | [Next in Thread] |