num2str not working for me
6 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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]';
0 commentaires
Réponses (2)
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
Walter Roberson
le 21 Oct 2013
eqtn = ['(x)^3 - ' num2str(P3) ' *(x^2) - ' num2str(P2) '*(x) - ' num2str(P1) ' = 0'];
0 commentaires
Voir également
Catégories
En savoir plus sur Symbolic Math Toolbox dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!