在MATLAB中使用surf函数绘制的3D图。

huangapple go评论89阅读模式
英文:

3D Plot with the surf function in MATLAB

问题

我想为下面给出的函数执行一些任务:

f(x) = A * cos(sqrt(x1.^2 + x2.^2)).^2 .* exp(-B * sqrt(x1.^2 + x2.^2))

我需要编写一个名为"sombrero"的函数,它应该接受:

  • x:一个包含2维坐标的向量;
  • const:一个包含常数A和B的2维向量。

然后:

  • 设置A = 2和B = 0.3
  • 计算在点x = (3, 2) 处的函数值;
  • 在[-10, 10]的正方形范围内绘制该函数。

当我尝试运行以下代码时,出现错误"Z must be a matrix, not a scalar or vector":

function fx = sombrero(x, const)
    A = const(1);
    B = const(2);

    r = sqrt(x(1)^2 + x(2)^2);
    fx = A * cos(r)^2 * exp(-B * r);
end

% Constants
A = 2;
B = 0.3;

% Coordinates
x = [3; 2];

% Evaluate the function at the given point
fx = sombrero(x, [A, B]);

% Define the range of x and y values
x_range = linspace(-10, 10, 100);
y_range = linspace(-10, 10, 100);

% Create a grid of points
[X, Y] = meshgrid(x_range, y_range);

% Evaluate the function at each point of the grid
Z = sombrero([X(:) Y(:)]', [A, B]);

surf(X,Y,Z)

错误发生在评估代码的最后一行。

英文:

I want to do some tasks for a given function below:

f(x) = A * cos(sqrt(x1.^2 + x2.^2)).^2 .* exp(-B * sqrt(x1.^2 + x2.^2))

I need to Write the function sombrero, which should accept

  • x: a 2-dimensional vector of coordinates;
  • const: a 2-dimensional vector with the value of the constants
    A and B.

and then

  • Set A = 2 and B = 0.3
  • obtain the value of the function, evaluated in the point
    x = (3, 2)′;
  • draw the function in the [-10, 10] square

When I try to run the code below I get this error "Z must be a matrix, not a scalar or vector"

function fx = sombrero(x, const)
    A = const(1);
    B = const(2);

    r = sqrt(x(1)^2 + x(2)^2);
    fx = A * cos(r)^2 * exp(-B * r);
end

% Constants
A = 2;
B = 0.3;

% Coordinates
x = [3; 2];

% Evaluate the function at the given point
fx = sombrero(x, [A, B]);

% Define the range of x and y values
x_range = linspace(-10, 10, 100);
y_range = linspace(-10, 10, 100);

% Create a grid of points
[X, Y] = meshgrid(x_range, y_range);

% Evaluate the function at each point of the grid
Z = sombrero([X(:) Y(:)]', [A, B]);

surf(X,Y,Z)

The error happens when evaluating the last line of code

答案1

得分: 0

您的问题在于sombrero函数中。

输入x是一个2xN矩阵,其中第一行是x坐标,第二行是y坐标。但您提取了x(1)x(2),这只给出了第一个点(数组的第一列)的坐标。相反,使用x(1,:)x(2,:)

接下来,因为您现在正在处理一个数组而不是标量,所以必须使用逐元素运算符。*是矩阵乘法,.*是逐元素乘法。同样适用于^.^,以及/./

最后,surf函数需要3个矩阵作为输入。XY是矩阵,但Z是一个向量,因为您将XY矩阵展平以将它们放入sombrero()中。我建议将它们保留为函数的单独输入,这样您就不需要展平它们以将它们放入矩阵中。

总的来说,函数应该如下所示:

function fx = sombrero(X, Y, const)
    A = const(1);
    B = const(2);

    r = sqrt(X.^2 + Y.^2);
    fx = A * cos(r).^2 .* exp(-B * r);
end

(我在那里保留了一些*,以便您可以根据需要乘以标量。)

现在,您可以如下调用该函数:

Z = sombrero(X, Y, [A, B]);
surf(X,Y,Z)
英文:

Your problem is in the sombrero function.

The input x is a 2xN matrix, where the first row is the x coordinate and the second row is the y coordinate. But you extract x(1) and x(2), which give you only the coordinates for the first point (the first column of the array). Instead, use x(1,:) and x(2,:).

Next, since you're now operating on an array instead of a scalar, you must use the element-wise operators. * is a matrix multiplication, .* is an element-wise multiplication. Likewise with ^ vs .^, and / vs ./.

Finally, surf requires 3 matrices as input. X and Y are matrices, but Z is a vector, because you flattened the X and Y matrices to put them into sombrero(). I would suggest keeping them as separate inputs to the function, so you don't need to flatten them to put them into a matrix.

All together, the function should look like this:

function fx = sombrero(X, Y, const)
    A = const(1);
    B = const(2);

    r = sqrt(X.^2 + Y.^2);
    fx = A * cos(r).^2 .* exp(-B * r);
end

(I've left some * in there where you multiply by a scalar.)

Now you'd call the function as follows:

Z = sombrero(X, Y, [A, B]);
surf(X,Y,Z)

huangapple
  • 本文由 发表于 2023年6月16日 05:50:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76485742.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定