How do I solve the following code?

조회 수: 7 (최근 30일)
Altin Guberi
Altin Guberi 2017년 5월 23일
댓글: Walter Roberson 2017년 5월 27일
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?

답변 (1개)

Walter Roberson
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
Altin Guberi
Altin Guberi 2017년 5월 27일
편집: Walter Roberson 2017년 5월 27일
I tried :
function Z = arrayfun( (x,y) ush25([x,y]), X, Y);
y = x(2); x = x(1);
z=3.*sin(0.5+0.25.*x.*y).*cos(x)
end
but i Get
Error: File: ush25.m Line: 1 Column: 24
Unbalanced or unexpected parenthesis or bracket.
Walter Roberson
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);

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by