필터 지우기
필터 지우기

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 CenterFile Exchange에서 Contour Plots에 대해 자세히 알아보기

태그

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by