MATLAB求解含积分的方程时出现Empty sym:0-by-1错误求助
Hey there! Let's break down why you're hitting that Empty sym: 0-by-1 error and fix your MATLAB code to solve the equation properly.
What's Causing the Error?
That error means vpasolve can't find a valid solution to your equation, and the root cause is how you're mixing symbolic and numerical functions here:
normcdfandnormpdfare numerical statistical functions, not fully compatible with MATLAB's symbolic integration (int). The symbolic toolbox can't compute an analytical closed-form for your integral, leavingeqnas an incomplete expression thatvpasolvecan't process.
How to Fix It: Switch to Numerical Methods
Since your equation involves a non-analytical integral, numerical solvers like fzero are a better fit. Here's a step-by-step corrected approach:
- Define a numerical function that calculates the left-hand side of your equation for any given
x(using numerical integration instead of symbolic). - Visualize the function to find a rough guess of where it crosses zero (critical for
fzeroto work). - Use
fzeroto find the exact root.
Corrected Code
a = 0.2; b = 10; c = -10; d = 15; mu = 3; sig = 1; % Define the numerical function for your equation f = @(x) a + b*normcdf(x, mu, sig) + c*integral(@(t) normcdf(d + x - t, mu, sig).*normpdf(t, mu, sig), 0, x); % Plot the function to find a valid initial guess for x x_vals = linspace(-5, 10, 100); y_vals = arrayfun(f, x_vals); figure; plot(x_vals, y_vals, 'LineWidth', 1.5); hold on; plot(x_vals, zeros(size(x_vals)), 'k--', 'LineWidth', 1); xlabel('x'); ylabel('f(x)'); title('f(x) = 0: Find Where the Line Crosses Zero'); hold off; % Solve using fzero (use a guess from the plot, e.g., x0=5) x_solution = fzero(f, 5); fprintf('Solution: x = %.4f\n', x_solution);
Key Notes
- The
integralfunction handles the numerical integration cleanly, pairing withnormcdf/normpdfwithout symbolic conflicts. - Always plot the function first! If you pick a bad initial guess for
fzero, it might fail or return an incorrect root. From the plot, you can see exactly wheref(x)crosses zero. - If you run into integration warnings, add
'ArrayValued', trueto theintegralcall to handle vector inputs more robustly.
内容的提问来源于stack exchange,提问作者Mike Lang
相关产品推荐
相关产品推荐

