Investing of a string/function and listing of variables

i want to make a program that separates the variables and coefficients from the entered function for example
the program will prompt user to enter a function if user enters 2x1+3x3 then the result/output should be var=[x1 x3] coefficients= [2 3]
if user enters x+y1+3y3-irt-2x2+3n then output should be var=[x y1 y3 irt x2 n] coefficients= [1 1 3 -1 -2 3] if user enters abcd then result should be var=[abcd] coefficients=[1] . I know its difficult but guys plz help me out. Thanx in advance.

Answers (1)

y=input('enter y','s')
y=sym(y)
[coefficient,var]=coeffs(y)
%you should enter y like below
12*x1+14*x2

4 Comments

Thanks very much.The problem I am facing now is in the listing variables. i used your codes the coefficient matrix is giving the desired results but variables matrix is not what is required. For example If I enter 2*x1+3*x3 the result using your code is coefficient=[2, 3] var = [ [x1,1], [1,x1 ]] Kindly help on that too.
I am not getting your result
waiting for your help Sir
>> y=input('enter y=','s')
y=sym(y)
[coefficient,var]=coeffs(y)
enter y=2*x1+3*x3
y =
2*x1 + 3*x3
coefficient =
[ 2, 3]
var =
[ x1, x3]

Sign in to comment.

Categories

Asked:

on 30 Apr 2013

Community Treasure Hunt

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

Start Hunting!