This is my code and I'm trying to get a contour plot for it. But I am not getting it. Can anyone help me
f=@(x,y) sin(x)+cos(y);
x0 = [5.5,5.5];
options = optimoptions('fminunc','Display','iter ','Algorithm','quasi-newton');
[opt, opt_cost]=fminunc('hw2pr2_cost',x0,options);
lb = (pi/2); ub = (2*pi);
options = optimoptions('fmincon','Display','iter','Algorithm','active-set');
[optx_con, opt_costcon] = fmincon('hw2pr2_cost',x0,[],[],[],[],lb,ub,'hw2pr2_constraint',options);
fcontour(f);
hold on
plot(opt(1),opt(2),'ko');
%contour(y,y,'ko');
hold on
plot(optx_con(1),optx_con(2),'ko');
%contour(y,y,'ko');
y=-x;
hold on
plot(x,y);

17 件のコメント

Aishwarya Bangalore Kumar
Aishwarya Bangalore Kumar 2018 年 2 月 14 日
there is no error for this but i cant seem to get the contour output
Birdman
Birdman 2018 年 2 月 14 日
Seen my answer for that question?
Aishwarya Bangalore Kumar
Aishwarya Bangalore Kumar 2018 年 2 月 14 日
yes it worked for that, but not this
Birdman
Birdman 2018 年 2 月 14 日
Okay, I get that this question is different, but why don't you let me know that whether it has worked for you or not in previous question?
Birdman
Birdman 2018 年 2 月 14 日
Where is hw2pr2_cost function?
Aishwarya Bangalore Kumar
Aishwarya Bangalore Kumar 2018 年 2 月 14 日
oh yes it did work in the previous question , there was an error with it. but this question has no error but i cant get the contour plot
Birdman
Birdman 2018 年 2 月 14 日
oh yes it did work in the previous question
Then you can let me know it worked by responding and accepting my answer.
Aishwarya Bangalore Kumar
Aishwarya Bangalore Kumar 2018 年 2 月 14 日
oh yes sure sir. Thank you for the help
Birdman
Birdman 2018 年 2 月 14 日
For this question, hw2pr2_cost function is needed. Share it please.
Aishwarya Bangalore Kumar
Aishwarya Bangalore Kumar 2018 年 2 月 14 日
function find_cost2 = hw2pr2_cost(x) find_cost2=sin(x(1))+cos(x(2)); end this is the function
Walter Roberson
Walter Roberson 2018 年 2 月 14 日
Difficult to debug without hw2pr2_cost, but mostly the problem is that you never assign a value to x before you do
y=-x
Aishwarya Bangalore Kumar
Aishwarya Bangalore Kumar 2018 年 2 月 14 日
so i should assign a vlue to x before running that?
Birdman
Birdman 2018 年 2 月 14 日
Also, hw2pr2_constraint?
Aishwarya Bangalore Kumar
Aishwarya Bangalore Kumar 2018 年 2 月 14 日
yes, and x(2)>4 is a condition that is specified function [cineq, ceq] = hw2pr2_constraint(x) ceq = []; if x(2) > 4 cineq = 0; else cineq = 4-x(2); end end
Walter Roberson
Walter Roberson 2018 年 2 月 14 日
Yes, you should assign a value to x before running that code. It is not at all clear what you are expecting, though. Are you trying to plot a 45 degree angle from top left to bottom right? If so then there are other ways of doing that.
your plot(x,y) is not going to be a contour plot. fminunc() and fmincon() are going to return vectors of length 2 that you are plotting as individual points on top of the fcontour() result. The function you are plotting through fcontour() has nothing obvious to do with whatever hw2pr2_cost does.
Aishwarya Bangalore Kumar
Aishwarya Bangalore Kumar 2018 年 2 月 14 日
got it thank you sir.

サインインしてコメントする。

回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で Contour Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by