fminsearch with multiple variables

I have an assignment that I have to use fminsearch to find minimum potential energy. I've seen similar questions asked all giving different styled answers but haven't had any luck getting it to work. Basically I'm given an equation for potential energy in respect to x and y. From (0:90) degrees I have to use the fminsearch to find minimum PE and x,y. I know this is a basic function but I've never used it before and it's giving me errors.
t=(0:90);
x=20.*sin(degtorad(t));
y=20.*cos(degtorad(t));
p = 4.*(x.^2)-0.1.*(x.^2)+4.*abs(y.^2).^(1.8);
Preferably I'd like my fminsearch to output x,y and the p values at once. Is this possible in just one function?

 Accepted Answer

fminsearch is only for application to function handles. For discrete variables:
[X, Y] = ndgrid(x, y);
p = 4.*(X.^2)-0.1.*(X.^2)+4.*abs(Y.^2).^(1.8);
[minval, minidx] = min(p);
best_x = X(minidx); best_y = Y(minidx); best_p = p(minidx);

7 Comments

Okay, and this may have been my first issue I should've asked. When trying to create an m.file function
t=(1:90);
function p =PE(t)
x=20.*sin(degtorad(t));
y=20.*cos(degtorad(t));
p= 4.*(x.^2)-0.1.*(x.^2)+4.*abs(y.^2).^(1.8);
end
gives the error "Function definitions are not permitted in this context" I explicitly need to use fminsearch for this portion of the assignment. Thanks for the quick response!
R2016b is the first version that allows script files (files that do not start with 'function' or 'classdef') to have functions stored in them. In all previous versions, functions must be stored in a different .m file than the script part.
But typically the easier thing to do is just stick
function NameOfYourMFile
at the top of what was a script.
function [best_x, best_y, best_p] = PE(t)
x=20.*sin(degtorad(t));
y=20.*cos(degtorad(t));
[X, Y] = ndgrid(x, y);
p = 4.*(X.^2)-0.1.*(X.^2)+4.*abs(Y.^2).^(1.8);
[minval, minidx] = min(p);
best_x = X(minidx); best_y = Y(minidx); best_p = p(minidx);
You might have noticed that there is no fminsearch there. That is because it does not make sense to use fminsearch on a discrete variable.
hint: if you have a function of two variables, f(x,y), that you want to use with fminsearch, then
fminsearch(@(xy) f(xy(1), xy(2)), TheInitialStartingPoint)
Not sure why we are required to use fminsearch but it's directly from Chapra's Numerical Methods text. So I just have to bite the bullet and use the method asked. I'm still getting the same issues with my function. My files is name PE and for trouble shooting I copied and pasted your code into a seperate file (PE_test) and changing yours accordingly. Still getting "Error: Function definitions are not permitted in this context." Very very frustrating. I'm going to troubleshoot some more.
Thanks for answering the main question, and I will definitely refer to it once i get this function bug taken care of.
For R2016a and earlier, the first non-comment in your PE.m has to be "function". If there is anything executable for that, you would get that error.
"Not sure why we are required to use fminsearch"
I suspect that you have not specified the problem correctly here. The way you have specified it here is that you are given a vector of t values, each of which corresponds to a degree angle on a circle of radius 20, and that the minimum is to be found from all combinations of x and y so implied.
Hmmm.... I guess I read in the need to find it over all of the combinations where it might not have been present.
function [best_x, best_y, best_p] = PE(t)
x=20.*sin(degtorad(t));
y=20.*cos(degtorad(t));
p = 4.*(x.^2)-0.1.*(x.^2)+4.*abs(y.^2).^(1.8);
[minval, minidx] = min(p);
best_x = x(minidx); best_y = y(minidx); best_p = p(minidx);
Now, what would make sense to use fminsearch over is the possibility that you did not know the t in advance and were asked to find it and you did not ask to output the x and y values and the function was only expected to be passed scalar t. In that combination of circumstances,
function p = PE(t)
x=20.*sin(degtorad(t));
y=20.*cos(degtorad(t));
p = 4.*(x.^2)-0.1.*(x.^2)+4.*abs(y.^2).^(1.8);
and then you fminsearch on PE . It does not need multiple parameters in this situation.
I see the issue you're talking about. I think that is where the confusion on my end of the question was. The question gave an equation with coeficients. Then the coefiecients are given to us so I prematurely just started evaluating the question instead of writing the function and then later inserting potential data given. I think that was the issue with my function and it seems to be working now. Thanks again! Matlab has some really cool operations not present in c++ but it can be really frustrating and nitpicky!

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!