필터 지우기
필터 지우기

num2str not working for me

조회 수: 6 (최근 30일)
Chris
Chris 2013년 10월 21일
댓글: sixwwwwww 2013년 10월 21일
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]';

답변 (2개)

sixwwwwww
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!
  댓글 수: 2
Chris
Chris 2013년 10월 21일
Thank you very much, that helps a lot. :)
sixwwwwww
sixwwwwww 2013년 10월 21일
you are welcome

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2013년 10월 21일
eqtn = ['(x)^3 - ' num2str(P3) ' *(x^2) - ' num2str(P2) '*(x) - ' num2str(P1) ' = 0'];

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by