How can I substitute a variable?
3 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
John D'Errico
2014-12-20
编辑:John D'Errico
2014-12-20
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]
0 个评论
更多回答(1 个)
Shoaibur Rahman
2014-12-20
a= 5;
syms x
y = a*sin(x).^2 ./(sin(x) + cos(x));
subs ( y,{x},{2} )
2 个评论
Shoaibur Rahman
2014-12-20
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)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Assumptions 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!