Subs Command for Multivariable Function

조회 수: 9 (최근 30일)
Alex Santizo
Alex Santizo 2020년 2월 7일
댓글: Walter Roberson 2020년 12월 11일
syms x y
g=(1/2)*e^(-2*x/3).*tan(y+1);
subs(g,x,0.3);
subs(g,y,-0.7);

답변 (2개)

Walter Roberson
Walter Roberson 2020년 2월 7일
For the case of scalars
subs(g, [x, y], [0.3,-0.7])
For the case of non-scalars
subs(g, {x, y}, {newx, newy})
  댓글 수: 3
Ashwin Shibu
Ashwin Shibu 2020년 12월 11일
i dont think there shd be s dot after e^(-2*x/3)
Walter Roberson
Walter Roberson 2020년 12월 11일
Dot between the e and the ^ is needed if x might be non-scalar. Dot between the ) and the *tan is needed if x and y might both be non-scalar.
That said, exp() is recommended instead of e^
g=(1/2)*exp(-2*x/3).*tan(y+1);
As a matter of style: I recommend that .* be used in any case involving variables unless you deliberately mean matrix multiplication (inner product). I would suggest * with scalars only for very simple constant multiples, such as 3*x -- but not, for example, x*y even if you know that x and y are both scalars. Using .* explicitly saves thinking by other people reading the code about whether you really meant matrix multiplication and saves programmers reading your code tracing back to prove that in every possible code path to that point, that x and y are guaranteed to be scalars.

댓글을 달려면 로그인하십시오.


Vladimir Sovkov
Vladimir Sovkov 2020년 2월 7일
If e^ is the exponent and if you want to estimate the result numerically (from the question it is not quite clear what you want), you can use
syms x y
g=(1/2)*exp(-2*x/3).*tan(y+1);
double(subs(subs(g,x,0.3),y,-0.7))
  댓글 수: 2
Alex Santizo
Alex Santizo 2020년 2월 7일
The question I am trying to solve is to evaluate that function at x & y are equal to the listed values using the subs command and by conversion into a matlab function.
Vladimir Sovkov
Vladimir Sovkov 2020년 2월 7일
편집: Vladimir Sovkov 2020년 2월 7일
This case the code above should work. Though, you can do it easier and with faster computation avoiding symbolic computations as, e.g. (there are many ways to do it)
function g = myfunc (x,y)
g=(1/2)*exp(-2*x/3).*tan(y+1);
end
and call it as
g=myfunc(0.3,-0.7)
The error in your code is because Matlab is unaware what the variable "e" is. If you mean the exponent function, use "exp" instead, as in the code above. If you actually defined "e" somewhere before, the error can be with nonscalar values; if you mean the element-wise operation, use ".^" instead of "^" analogous to ".*" in the following part.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by