How do I solve the following code?
5 次查看(过去 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!