MATLAB Answers

Subs Command for Multivariable Function

10 views (last 30 days)
Alex Santizo
Alex Santizo on 7 Feb 2020
Edited: Vladimir Sovkov on 7 Feb 2020
syms x y
g=(1/2)*e^(-2*x/3).*tan(y+1);
subs(g,x,0.3);
subs(g,y,-0.7);

  0 Comments

Sign in to comment.

Answers (2)

Walter Roberson
Walter Roberson on 7 Feb 2020
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})

  1 Comment

Alex Santizo
Alex Santizo on 7 Feb 2020
For some reason I keep getting an error in this line of code.
g=(1/2)*e^(-2*x/3).*tan(y+1);
%% Says there is an error on this line and Im not understanding where.

Sign in to comment.


Vladimir Sovkov
Vladimir Sovkov on 7 Feb 2020
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 Comments

Alex Santizo
Alex Santizo on 7 Feb 2020
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 on 7 Feb 2020
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!