force matlab to display its symbolic calculations using indexed varaiables

2 次查看(过去 30 天)
hello. I'm looking for a way to make matlab display the result of its output by showing the variables with their index, while doing symbolic calculations. for example in this code I want it to display x(1) instead of x1.
x=sym('x',[1 2])
y=x(1)*x(2)
e=diff(y,x(2))
now here's matlab results:
ans=
x1
what matlab did here, was to show what's contained in x(1) which is x1. the reason why I'm trying to do this, is that I need these results to create a function whose input is an indexed variable to use with fsolve because this function requires x(1), x(2) and not x1, x2. I know it is possible using strings and eval function but I need a better way.

采纳的回答

Walter Roberson
Walter Roberson 2017-1-12
x = sym('x',[1 2])
y = x(2)^2 + sin(x(1)*x(2));
e = diff(y, x(2))
f = matlabFunction(y, 'vars', {x});
fsolve(f, rand(1,2))
  2 个评论
hosein Javan
hosein Javan 2017-1-12
thank's. I'm still interested to know how this worked. when you run this code:
x=sym('x',[1 2])
e=x(1)+x(2)
f = matlabFunction(e, 'vars', {x})
the result gives this:
f =
@(in1)in1(:,1)+in1(:,2)
could you explain how I should have known this? I didn't find any similiar example in matlab's help. it seems it will automatically vectorize inputs.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by