num2str not working for me
6 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(2 个)
sixwwwwww
2013-10-21
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!
Walter Roberson
2013-10-21
eqtn = ['(x)^3 - ' num2str(P3) ' *(x^2) - ' num2str(P2) '*(x) - ' num2str(P1) ' = 0'];
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Symbolic Math Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!