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
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
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]
z = [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
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]
z = [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
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 Center 和 File Exchange 中查找有关 Graphics Performance 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
