How can I substitute a variable?

Dear community,
How can I use 'subs' here?
I defined x array to interpolate y and here I want to substitute x= 2 into y . By the way, how can I substitute x into y?
a= 5;
x = 0:pi/10:pi;
y = a*sin(x).^2 ./(sin(x) + cos(x));
subs ( y,{x},{2} ) --> it won't work by doing like this
Thanks in advance

 采纳的回答

So why not use subs? It works fine, as long as you use it as designed.
syms x
a = 5;
% I don't need the dot operators in a symbolic expression,
% but they do work too.
y = a*sin(x)^2 /(sin(x) + cos(x))
y =
(5*sin(x)^2)/(cos(x) + sin(x))
subs(y,x,1:pi/10:pi)
ans =
[ (5*sin(1)^2)/(cos(1) + sin(1)), (5*sin(739805897222023/562949953421312)^2)/(cos(739805897222023/562949953421312) + sin(739805897222023/562949953421312)), (5*sin(458330920511367/281474976710656)^2)/(cos(458330920511367/281474976710656) + sin(458330920511367/281474976710656)), (5*sin(1093517784823445/562949953421312)^2)/(cos(1093517784823445/562949953421312) + sin(1093517784823445/562949953421312)), (5*sin(317593432156039/140737488355328)^2)/(cos(317593432156039/140737488355328) + sin(317593432156039/140737488355328)), (5*sin(1447229672424867/562949953421312)^2)/(cos(1447229672424867/562949953421312) + sin(1447229672424867/562949953421312)), (5*sin(812042808112789/281474976710656)^2)/(cos(812042808112789/281474976710656) + sin(812042808112789/281474976710656))]
Not very easy to read that way, or that useful, so use vpa to improve things a bit...
vpa(subs(y,x,1:pi/10:pi))
ans =
[ 2.5621910014165363100864295707477, 3.8309204938871849945381706934322, 5.2967473394750604192098370433573, 7.6345151542111843907753899746208, 21.303646775422896588787558509401, -4.8465627759522670140582286462245, -0.45155218609854555823864593466888]

更多回答(1 个)

a= 5;
syms x
y = a*sin(x).^2 ./(sin(x) + cos(x));
subs ( y,{x},{2} )

2 个评论

Moj
Moj 2014-12-20
编辑:Moj 2014-12-20
I've written there, I used interpolation in the last step, so by defining syms x I can't deal with interpolation process!
subs is a symbolic substitution in Matlab. So, what if you use any one of the following techniques:
a= 5;
x = 0:pi/10:pi;
y = a*sin(x).^2 ./(sin(x) + cos(x));
interp1(x,y,2)
This approximates a value of y at x = 2, but may not exact. Second, you can define y as function of x, then replace x by any value. In this case you can expect an exact value.
a = 5;
y = @(x) a*sin(x).^2 ./(sin(x) + cos(x));
y1 = y(0:pi/10:pi);
y2 = y(2)

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Creating, Deleting, and Querying Graphics Objects 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by