How do I solve the following code?

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?

Answers (1)

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 Comments

On the exercise it says only to create a matlab file of the type z=ush25v(x) as a function of vectorial variable x=[x(1) , x(2)] =[x,y]
So maybe I need to change x and y at the contour also.
but do you think there is anything i can change at Ush25.m function?
Z = arrayfun(@(x,y) ush25([x,y]), X, Y);
Now the function would expect a single input which would be a vector of two values, that you could then separate inside the code
y = x(2); x = x(1);
Should I put @ ? because i get unexpected matlab operator?
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.
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);

Sign in to comment.

Categories

Find more on Graphics Objects in Help Center and File Exchange

Asked:

on 23 May 2017

Commented:

on 27 May 2017

Community Treasure Hunt

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

Start Hunting!