This supposed solver does not work. What am I missing?
3 次查看(过去 30 天)
显示 更早的评论
Description. S = solve( eqn , var ) solves the equation eqn for the variable var . If you do not specify var , the symvar function determines the variable to solve for. For example, solve(x + 1 == 2, x) solves the equation x + 1 = 2 for x.
actually, I will need to solve for a variable in a much more complex equation but can not even make this work
my code is as follows
syms x
solve(x + 1 == 2, x)
disp(x)
% Instead it throws this error:
Error in solv (line 1)
solve(x + 1 == 2, x)
BTW
(I would never say this is urgent) %and thanks
0 个评论
回答(1 个)
Steven Lord
2022-7-7
From the fact that the error is being thrown on line 1 of your file, I'm guessing you haven't defined the variable x. The message you posted does include the syms call that should have created it, but it's not in the code block so I'm guessing you didn't execute it. Try defining x as a symbolic variable by running that syms call then rerun your solve call.
If that doesn't resolve the problem please show us the full and exact text of the error message (all the text displayed in red in the Command Window)? The full text may be useful in determining what's going on.
4 个评论
Star Strider
2022-7-7
If you want to display the solved value, it needs to be assigned to something to use the disp funciton —
syms x
xsol = solve(x + 1 == 2, x)
disp(xsol)
If that does not work, use the ver command to see if you have the Symbolic Math Toolbox installed and activated.
.
Steven Lord
2022-7-7
So I'm guessing you added the syms command to your script and now it's erroring when MATLAB tries to run that command. If that's the case I think @Star Strider is correct to suspect that you don't have Symbolic Math Toolbox installed (or if it is installed no license is available right now.)
If you don't have and can't get access to Symbolic Math Toolbox, for problems with one equation and one unknown using fzero from MATLAB may be an option. You specify the problem a bit differently than you would with solve from Symbolic Math Toolbox, but the documentation page has a number of examples you can use as a model.
If you want more than one equation and/or more than one unknown, check if you have Optimization Toolbox. If you do you can use fsolve which is closer to fzero than to solve in syntax.
fzero(@(x) x + 1 - 2, 0) % Find a zero of x + 1 - 2 == 0 or x + 1 == 2
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!