Reputation: 14573
I have recently started programming in MATLAB to create some utilities for myself. I want to create a graphical interface for solving equations.
In the command line I can achieve this simply by:
syms x
result = solve('12*x=24');
( and result is going to be = 2 )
I wanted to improve this process by creating a GUI.
The current program I have looks like this:
function [ output_args ] = SolveEq( equation )
syms x a b;
output_args = solve( equation );
end
This works only for equation that work on x
, a
and b
variables. I want to process the string as follows:
Get the variables out of that string.
for example: in string '12+a-b=0'
, I want my program to be able to find that a
and b
are variables, and also perfom the syms
operation for them. How can I achieve this?
Upvotes: 0
Views: 205
Reputation: 36710
The syntax you are using works without declaring symbolic variables:
f=solve('12*x=24');
Working with expressions instead of strings require to declare symbolic variables:
syms x
f=solve(12*x==2);
Use strings, then nothing must be done.
Code you need after solving:
if isstruct(f)
%more than one variable
names=fieldnames(f);
else
%only one variable, f is the solution
end
Upvotes: 2