How do I solve the following code?
조회 수: 7 (최근 30일)
이전 댓글 표시
Hello
I have the following code:
x=0:.1:5; y=0:.1:8;
[X Y]=meshgrid(x,y);
Z=ush25(X,Y);
surf(X,Y,Z)
figure
contour(X,Y,Z)
figure
contour(X,Y,Z,50)
figure
contour(X,Y,Z,[2 2.55],'r')
and the function code is:
function z=ush25(x,y)
x=[x(1),x(2)]=[x,y]
z=3.*sin(0.5+0.25.*x.*y).*cos(x)
end
So, what I have to do is to put x, y in a vector x , as a result x> x(1) and y=x(2)
Any idea how to do that?
댓글 수: 0
답변 (1개)
Walter Roberson
2017년 5월 23일
The sizes would not work out if you did that. You are passing in 81 x 51 matrices X and Y, which become known by the names x and y in the function. Inside the function, you want to replace x with a vector of two elements. Then you have
z=3.*sin(0.5+0.25.*x.*y).*cos(x)
with x now replaced by a vector of two elements, x.*y would be either 1 x 2 or 2 x 1 (depending on row or column vector) times an 81 x 51 matrix, and that cannot work.
But perhaps you did not want to replace the first x in that expression, only the second. In that case you would have 3.*sin(0.5+0.25.*x.*y) be an 81 x 51 result, and you would be trying to multiply it by cos() of either a 1 x 2 or 2 x 1, and that cannot work for size reasons either.
I am not at all clear as to why you think you need to construct that vector? If you just leave that line out then you get a reasonable result.
댓글 수: 5
Walter Roberson
2017년 5월 27일
No, go back to the previous way of defining your function. Replace
Z=ush25(X,Y);
with
Z = arrayfun(@(x,y) ush25([x,y]), X, Y);
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!