Calculating the mathematical expression

3 次查看(过去 30 天)
I have to calculate the value of an expression (x) for a range of inputs y and z and plot the output separately with respect to y and z. How do I do this?
y = [1,1000,50];
z = [1,1000,50];
x = 55*y^(3/2) + 10*z^(4);
  3 个评论
Torsten
Torsten 2022-3-7
Sure. You can also use a loop:
n = numel(y);
x = zeros(1,n);
for i = 1:n
x(i) = 55*y(i)^(3/2) + 10*z(i)^(4);
end
x

请先登录,再进行评论。

采纳的回答

John D'Errico
John D'Errico 2022-3-7
编辑:John D'Errico 2022-3-7
There are two possible questions in this.
You have a vector y, and a vector z. You wish to evaluate the expression for x, for ALL combinations of x and y, then plot the result. Think of this as like a multiplication or addition table. You do it by insuring that y and z are both vectors, but that one is a row vector, and the other a column vector.
I won't do your exact problem, since it might be your homework assignment. But it might look like this:
y = [1 2 3 4 5]
y = 1×5
1 2 3 4 5
z = [2 3 5 7 11 13]'
z = 6×1
2 3 5 7 11 13
Do you see that y is a row vector, and y is a column vector? I left the semi-colons off the end of the lines so you will see that. Now, if I compute some expression using the .* operators, (also the .^ and ./ operators, there is no need to use a dot in front of the addition and subtraction operators) then we will get a nice matrix as a result. For example:
x = y - y.*z + z.^3
x = 6×5
7 6 5 4 3 25 23 21 19 17 121 117 113 109 105 337 331 325 319 313 1321 1311 1301 1291 1281 2185 2173 2161 2149 2137
MATLAB has evaluated the expression for all combinations of the two vector elements, so we had x(i,j)=f(y(j),z(i)). You can now simply plot the resulting surface, using surf.
surf(y,z,x)
xlabel y
ylabel z
zlabel x
box on
But suppose you just wanted to form the element-wise function of each pair, taken in the form x(i)=f(y(i),z(i))? Now x and y must be vectors of the same length. They must also be both row and column vectors.
y = [1 2 3 4 5]
y = 1×5
1 2 3 4 5
z = [2 3 5 7 11]
z = 1×5
2 3 5 7 11
Here, we see that y and z were both row vectors, both of length 5 in this case.
x = y - y.*z + z.^3
x = 1×5
7 23 113 319 1281
The reason you use the dotted opertors, is because the operation u*v is used to compute an operation in linear algebra, a dot product between vectors.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Graphics Performance 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by