Clear Filters
Clear Filters

force matlab to display its symbolic calculations using indexed varaiables

1 view (last 30 days)
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.

Accepted Answer

Walter Roberson
Walter Roberson on 12 Jan 2017
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 Comments
hosein Javan
hosein Javan on 12 Jan 2017
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.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!