How do I solve the following code?

7 Ansichten (letzte 30 Tage)
Altin Guberi
Altin Guberi am 23 Mai 2017
Kommentiert: Walter Roberson am 27 Mai 2017
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?

Antworten (1)

Walter Roberson
Walter Roberson am 23 Mai 2017
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 Kommentare
Altin Guberi
Altin Guberi am 27 Mai 2017
Bearbeitet: Walter Roberson am 27 Mai 2017
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 am 27 Mai 2017
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);

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by