Plotting a contour of a function with one input variable

8 ビュー (過去 30 日間)
Rachel Dodson
Rachel Dodson 2021 年 11 月 2 日
編集済み: Chris 2021 年 11 月 2 日
Hi
My function essentially has 2 inputs , x and y, but they're in the form theta = (x;y). So the first line of my code is x = theta(1,1), y = theta(2,1), and my function takes one input theta which should be a 2x1 matrix.
I need to plot a contour of the function with x and y taking certain values. But when I use "fcontour" I get 'not enough input arguments'. I'm not able to change the number of inputs for my function, it has to take a single input theta.
Any help would be appreciated for the contour plot! Thank you

回答 (1 件)

Chris
Chris 2021 年 11 月 2 日
編集済み: Chris 2021 年 11 月 2 日
exes = linspace(-2*pi,2*pi);
whys = exes;
theta = [exes',whys'];
takeOneInput(theta)
You can pack as much as you want into an input, depending on the data type. This one uses fcontour.
theta2.fun = @(x,y) sin(x) + cos(y);
theta2.xyinterval = [-2*pi,2*pi, -2*pi, 2*pi]
theta2 = struct with fields:
fun: @(x,y)sin(x)+cos(y) xyinterval: [-6.2832 6.2832 -6.2832 6.2832]
takeAnotherInput(theta2)
function takeOneInput(theta)
[x,y] = meshgrid(theta(:,1),theta(:,2));
f = sin(x) + cos(y);
contour(theta(:,1),theta(:,2),f);
end
function takeAnotherInput(theta)
fcontour(theta.fun,theta.xyinterval);
end
But if you are designing the function, why is it only permitted to take one input?

カテゴリ

Help Center および File ExchangeContour Plots についてさらに検索

タグ

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by