num2str not working for me

6 vues (au cours des 30 derniers jours)
Chris
Chris le 21 Oct 2013
Commenté : sixwwwwww le 21 Oct 2013
I'm doing a simple MATLAB problem where I have a nonlinear equation of root three, and the coefficients aren't explicitly given, they're found through formulas. I'm trying to display the equation so that I can use the solve function to find the three solutions to the equation, but I can't get the num2str function to work properly for me. When I input it into the editor, there's a red line below the first num2str function, but no on the latter two.
P1, P2, and P3 are all found through formulas given, and the values are known. I got that part to work fine, and I'm getting normal numbers for the coefficient values. I'm just trying to format it so that if I were to change the values that govern P1, P2, and P3, I wouldn't have to change the format of the equation.
Any help would be appreciated.
eqtn = '[(x)^3 - ' num2str(P3) ' *(x^2) - ' num2str(P2) '*(x) - ' num2str(P1) ' = 0]';

Réponses (2)

sixwwwwww
sixwwwwww le 21 Oct 2013
Dear Chris, you don't need to use num2str function in order to achieve your goal. You can solve this equation and also able to change values of P1, P2 and P3 by using symbols in MATLAB in the following way:
syms x P1_sym P2_sym P3_sym % Define symbols which make the equation
eqn_sym = x^3 - P3_sym * x^2 - P2_sym * x - P1_sym; % Your equation
P1 = 10; % Assumed
P2 = 20; % values of
P3 = 30; % P1, P2 and P3
eqn = subs(eqn_sym, [P1_sym P2_sym P3_sym], [P1 P2 P3]); % Putting values of P1, P2 and P3 in equation
solution = double(solve(eqn, x)) % Solving the equation and getting result
I hope it helps and is much convenient. Good luck!
  2 commentaires
Chris
Chris le 21 Oct 2013
Thank you very much, that helps a lot. :)
sixwwwwww
sixwwwwww le 21 Oct 2013
you are welcome

Connectez-vous pour commenter.


Walter Roberson
Walter Roberson le 21 Oct 2013
eqtn = ['(x)^3 - ' num2str(P3) ' *(x^2) - ' num2str(P2) '*(x) - ' num2str(P1) ' = 0'];

Catégories

En savoir plus sur Symbolic Math Toolbox dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by