Contour Plot of Rosie Function

Hello,
I am trying to get the same plot as my professor showed in class. He said to use a for loop instead of meshgrid. I keep getting an error that X must not be a scalar. I'm not sure where I'm going wrong. Below is the plot I'm supposed to get followed by my code that is not working.
i=1; j=1;
x1=-1:.01:3;
x2=-1:.01:3;
for a=x1
for b=x2
f(i,j)=100*(a^2-b)^2+(1-a)^2;
j=j+1;
end
j=1;
i=i+1;
end
clf;figure(1)
contour(a,b,f)

3 件のコメント

Adam Danz
Adam Danz 2021 年 1 月 24 日
編集済み: Adam Danz 2021 年 1 月 24 日
The error means that 'a' and 'b' cannot be scalars (single values) in contour(a,b,f).
Look at the values of the inputs and it should be clear that a contour could not possibly be created based on those values.
You probably mean
contour(x1,x2,f)
but that doesn't reproduce the contour plot in the image.
Brittany Burns
Brittany Burns 2021 年 1 月 24 日
編集済み: Brittany Burns 2021 年 1 月 24 日
I did mean to use x1 and x2 instead of a and b. After that, I'm stuck on getting the correct contour. I've been able to fiddle with it and hand the contours directly to contour and get what's below, but that still is not correct.
i=1; j=1;
conts=[-1 -.5 0 .5 2 3 3.5 3.6 3.7 3.8 3.9 3.98 4 4.02 4.1 4.2 4.5 5 10 ];
x1=-1:.01:3;
x2=-1:.01:3;
for a=x1
for b=x2
f(i,j)=100*(a^2-b)^2+(1-a)^2;
j=j+1;
end
j=1;
i=i+1;
end
clf;figure(1)
contour(x1,x2,f,conts)
Adam Danz
Adam Danz 2021 年 1 月 24 日
I'm sure it's based on some dataset, function, or algorithm that our prof must have provided at some point.

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

回答 (1 件)

Star Strider
Star Strider 2021 年 1 月 24 日

0 投票

Transpose ‘f’, since ‘x1’ and ‘x2’ appear to be the same (otherwise it would also be necessary to reverse their orders in the argument list for the contour call):
i=1; j=1;
% conts=[-1 -.5 0 .5 2 3 3.5 3.6 3.7 3.8 3.9 3.98 4 4.02 4.1 4.2 4.5 5 10 ];
conts = 10.^(-1:1:3);
x1=-1:.01:3;
x2=-1:.01:3;
for a=x1
for b=x2
f(i,j)=100*(a^2-b)^2+(1-a)^2;
j=j+1;
end
j=1;
i=i+1;
end
% clf;
figure(1)
contour(x1,x2,f.',conts, 'ShowText','on', 'LineWidth',1.5)
axis('equal')
.

カテゴリ

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

質問済み:

2021 年 1 月 24 日

回答済み:

2021 年 1 月 24 日

Community Treasure Hunt

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

Start Hunting!

Translated by