Help with 'solve' function in MATLAB for numerical solution!

For example, lets say I have the expression "x=2^x*b+c" and I use solve function as,
d=solve('x=(2^x)*b+c');
Now if I were to assign 'b' and 'c' values prior to writing the solve statement, the solution in 'd' will still return a SYMBOLIC solution.
If b=2, c=3, for example and I write,
d=solve('x=(2^x)*2+3');
I will get the numerical solution, but INSTEAD
if i type
d=solve('x=(2^x)*b+c');
with the values of 'b' and 'c' declared prior to this statement, I get a symbolic solution.
What do I need to do to get a NUMERICAL answer each time? (This is because the values of 'b' and 'c' are changing within a loop) (Also note this is just an example to illustrate my problem, not the real function I wish to solve)
Thanks Ian

 Akzeptierte Antwort

Matt Fig
Matt Fig am 5 Sep. 2012
Bearbeitet: Matt Fig am 5 Sep. 2012
d = solve('x=(2^x)*b+c');
n = subs(d,{'b','c'},{2,3}) % Put 2 for b, 3 for c
If you are doing the substitutions in a loop, this will probably be faster:
C = matlabFunction(d); % Convert symbolic into a func handle.
C(2,3)

3 Kommentare

Ian
Ian am 6 Sep. 2012
Matt I tried it, but its giving me an [empty sym]
This is the actual function: d=solve('x=2^(1+(b-3)/(3*2^x))-c','x');
And I get d=[empty sym] Even if I carry out n=subs(d,{'b','c'},{2,3}) I still get []
But if the values were inserted into the solve equation like so, d=solve('x=2^(1+(2-3)/(3*2^x))-3','x') then I get d=-2.41....
Any way to work around this? Thanks Ian
Matt Fig
Matt Fig am 6 Sep. 2012
Bearbeitet: Matt Fig am 6 Sep. 2012
Yes, that is a different problem isn't it?!
When symbolics won't work, go numerical...
b = 2;
c = 3;
f = @(x) x - (2.^(1+(b-3)./(3*2.^x))-c);
fzero(f,1)
ans =
-2.4184
Ian
Ian am 12 Sep. 2012
thank you!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Babak
Babak am 5 Sep. 2012
After you get the symbolic solution "d", you may need to say:
num_sol = vpa(d)
which computes d with numbers provided before. You should look at the list of the functions provided in Symbolic Math Toolbox.
Heidi Miller
Heidi Miller am 16 Sep. 2020

0 Stimmen

%%
%
syms x y g
x=x==6
x=6
g = 3*pi*x.^2
gives the numerical answer too

1 Kommentar

The original question had x on both sides of the equation.
The code you post here does not have x on both sides of the equation. It also overwrites values in such a way that the code is completely equivalent to just the last two lines, assigning numeric 6 to x and then calculating g purely numerically. No equation solving is performed.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Symbolic Math Toolbox finden Sie in Hilfe-Center und File Exchange

Gefragt:

Ian
am 5 Sep. 2012

Kommentiert:

am 16 Sep. 2020

Community Treasure Hunt

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

Start Hunting!

Translated by