채택된 답변

Star Strider
Star Strider 2015년 10월 9일

1 개 추천

You have to give fminsearch a function of one parameter vector. I created two versions of your ‘fun’ function, one to use as an argument to fminsearch and one for the plot, vectorising both functions so they will work:
% MAPPING: b(1) = x, b(2) = y
fun=@(b)100*(b(2)-b(1).^2).^2+(1-b(1)).^2; % Argument To ‘fminsearch’
[z,val]=fminsearch(fun,[3,3])
x=-10:1:10;
y=-10:1:10;
[X,Y] = meshgrid(x,y); % Create Matrix Arguments
funplot=@(x,y)100*(y-x^2)^2+(1-x)^2; % Plotting Version
figure(1)
surfc(X, Y, funplot(X,Y))
grid on
I could have used the same function for both, but this is more straightforward.

댓글 수: 5

Stephen23
Stephen23 2015년 10월 9일
편집: Stephen23 2015년 10월 10일
To create the function in one variable from the plotting function in two variables:
funplot = @(x,y)100*(y-x.^2).^2+(1-x).^2;
funfmin = @(v)funplot(v(1),v(2));
This has the advantage that the "actual" function definition is written using the explicit variable names x and y, which may be clearer to understand. EDIT: vectorize funplot.
Star Strider
Star Strider 2015년 10월 9일
Good point. Early in the day for me here.
Shawn Miller
Shawn Miller 2015년 10월 10일
편집: Shawn Miller 2015년 10월 10일
Thank you both, just three more things:
@Star Strider, I think your "funplot" should have three more dots behind each ^ symbol for element-wise operation and 3-D plotting, right? Also, what does figure(1) do?
@Stephen, as you defined funfmin = @(v)funplot(v(1),v(2)) from funplot, I wonder if this implicitly confines v to be a 2 by 1 vector and v(1) and v(2) to be scalars (since v (not a cell array) can contain neither two vectors nor two matrices as v(1) and v(2))? In other words, the way that fminsearch use vector parameter as input inevitably restricts us from using vector/matrices inputs, right?
Star Strider
Star Strider 2015년 10월 10일
편집: Star Strider 2015년 10월 10일
@Shawn — Yes it should. I miscopied it somehow. (The code worked and produced the correct plot with the correct element-wise function operators.) It should look like ‘fun’:
funplot=@(x,y)100*(y-x.^2).^2+(1-x).^2; % Plotting Version
EDIT — To plot the minimum as well as the function, the code becomes:
fun=@(b)100*(b(2)-b(1).^2).^2+(1-b(1)).^2; % Argument To ‘fminsearch’
[z,val]=fminsearch(fun,[3,3])
x=-10:1:10;
y=-10:1:10;
[X,Y] = meshgrid(x,y); % Create Matrix Arguments
funplot=@(x,y)100*(y-x.^2).^2+(1-x).^2; % Plotting Version
figure(1)
surfc(X, Y, funplot(X,Y))
hold on
plot3(z(1), z(2), funplot(z(1),z(2)), 'mp', 'MarkerSize',15, 'MarkerFaceColor','c')
hold off
grid on
view(-15, 30)
It plots the minimum with a slightly buried magenta pentagram with a red outline.
Stephen23
Stephen23 2015년 10월 10일
@Shawn Miller: one can vectorize funplot, and this can be called (and plotted) with vector/matrix inputs. funfmin stays the same.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Stalin Samuel
Stalin Samuel 2015년 10월 9일

0 개 추천

yrfun = @(x)100*(x(2)-x(1)^2)^2+(1-x(1))^2;
[z,val]=fminsearch(yrfun,[-10 10]);

댓글 수: 1

Shawn Miller
Shawn Miller 2015년 10월 9일
편집: Shawn Miller 2015년 10월 9일
Thanks Stalin, I actually know that using a vector x in the anonymous function works, however, the problem appeared when I tried to plot the fun, and it makes it difficult to plot using vector x I suspect. This is why I turn to use x and y as independent variables. Do you know if fminsearch supports function defined in my way/if you don't think so, how do you plot the function using vector x as input?

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Function Creation에 대해 자세히 알아보기

태그

질문:

2015년 10월 9일

댓글:

2015년 10월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by