MATLAB拟合方程求解遇问题:fit_eq为char类型需转sym
Let's break down why you're getting Empty sym: 0-by-1 and walk through reliable fixes for your MATLAB code:
Root Causes of the Issue
Your current workflow uses string replacement to convert the fit formula into a character string, then tries to solve it directly. This fails for two key reasons:
- Locale-Based Format Errors: If your system uses German locale,
num2stroutputs decimal commas (,instead of.), which the Symbolic Math Toolkit doesn't recognize as valid decimal separators. - Unreliable String Manipulation: Manual string replacement is prone to subtle bugs (like accidental partial matches or formatting quirks) that break the symbolic expression structure.
Solution 1: Build Symbolic Expression Directly from Fit Object (Recommended)
Skip string operations entirely by constructing the symbolic equation using the fit object's native coefficients and terms. This is far more robust:
% Assuming fitobject_{1} is already generated from your fitting loop syms x y % Extract coefficients and their corresponding polynomial terms coeff_values = coeffvalues(fitobject_{1}); coeff_terms = coeffnames(fitobject_{1}); % Build the symbolic polynomial step-by-step fit_symbolic = 0; for idx = 1:length(coeff_values) % Convert term name (e.g., 'x^9') to a symbolic term symbolic_term = str2sym(coeff_terms{idx}); % Add the term multiplied by its coefficient to the equation fit_symbolic = fit_symbolic + coeff_values(idx) * symbolic_term; end % Solve for x when y=1 (use 'Real', true to filter for only real solutions) erg = solve(fit_symbolic == y, x, 'Real', true);
This method leverages MATLAB's native symbolic tools, eliminating all risks associated with string formatting errors.
Solution 2: Fix the String-to-Symbol Conversion
If you want to stick with your original string-based approach, fix the decimal separator issue and ensure proper conversion to a symbolic expression:
% ... your existing code up to the fit_eq generation loop ... for ii=1:1:numel(cvalues) cname = cnames{ii}; cvalue = num2str(cvalues(ii)); % Replace German decimal commas with periods for symbolic compatibility cvalue = strrep(cvalue, ',', '.'); fit_eq = strrep(fit_eq, cname , cvalue); end % Convert the cleaned string to a symbolic expression fit_symbolic = str2sym(fit_eq); syms x y=1; % Solve with real solution filtering (critical for odd-degree polynomials) erg = solve(fit_symbolic == y, x, 'Real', true);
Why This Works
- The decimal comma replacement ensures the symbolic engine interprets coefficients correctly.
- Adding
'Real', truetellssolveto return only real solutions—critical for a 9th-degree polynomial, which will have at least one real root per the Fundamental Theorem of Algebra.
Additional Notes
- A 9th-degree polynomial can have up to 9 real roots, so
ergmay return a vector of solutions instead of a single value. - Always verify your symbolic expression with
disp(fit_symbolic)before solving to catch any remaining formatting issues.
内容的提问来源于stack exchange,提问作者Cassiopeia

