num2str not working for me
3 ビュー (過去 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 で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!