含超几何函数的MATLAB绘图代码报错,求问题排查
Hey there, let's work through the bugs in your code step by step. I've spotted several syntax and logical issues that are causing the errors, even after the edits from @Doctor Core and @Nakini. Here's a breakdown of the fixes and a fully corrected version:
1. Undefined Variable P_d
You commented out P_d = 1; but your code references P_d multiple times (in (P_c/P_d).^delta and lambda_d(k).*P_d/P_c). Uncomment this line to define the variable—MATLAB throws an error when it encounters an undefined variable.
2. Invalid Tplot Definition
Your initial linspace(-7.5, 10, 2.5) is invalid because the third argument to linspace must be an integer (number of points). The revised colon notation also won't generate a proper logarithmic spaced array. Instead, use MATLAB's logspace function, which is built for creating evenly spaced values on a log scale:
Tplot = logspace(-7.5/10, 10/10, 100); % 100 smooth points between 10^(-0.75) and 10^1
3. Mismatched Parentheses in p_c Calculation
The line for p_c(k,:) has an extra closing parenthesis at the end, which breaks the syntax. Remove the final ) to fix this:
Original (broken):
p_c(k,:) = lambda_c./((lambda_c*(mu + 1) + lambda_d(k).*P_d/P_c).^delta).*v);
Fixed:
p_c(k,:) = lambda_c./((lambda_c*(mu + 1) + lambda_d(k).*P_d/P_c).^delta) .* v;
4. Array Dimension Consistency
Since Tplot is a row vector (from logspace), mu and v will also be row vectors. Assigning a row vector to p_d(:,k) (a column vector) causes a dimension mismatch. Instead, use row indices for both p_d and p_c, and initialize the matrices upfront for clarity:
p_d = zeros(length(lambda_d), length(Tplot)); p_c = zeros(length(lambda_d), length(Tplot)); for k = 1:length(lambda_d) p_d(k,:) = lambda_d(k) ./ ((lambda_d(k) .* (v + 1) + lambda_c .* (P_c/P_d).^delta) .* v); p_c(k,:) = lambda_c ./ ((lambda_c*(mu + 1) + lambda_d(k).*P_d/P_c).^delta) .* v; end
5. Symbolic to Numeric Conversion for hypergeom
MATLAB's hypergeom (from the Symbolic Math Toolbox) returns a symbolic array. To avoid mixing symbolic and numeric data in calculations, wrap the result in double() to convert it to a numeric array:
mu = (delta/(1-delta)) .* Tplot .* double(hypergeom([1, 1-delta], 2-delta, -Tplot));
Full Corrected Code
alpha = 4; delta = 2/alpha; P_c = 1; P_d = 1; % Uncommented to define the required variable T = 1; % Proper logarithmic spaced x-axis values Tplot = logspace(-7.5/10, 10/10, 100); lambda_c = 0.01; lambda_d = [0.002, 0.01, 0.05]; % Convert symbolic hypergeometric output to numeric mu = (delta/(1-delta)) .* Tplot .* double(hypergeom([1, 1-delta], 2-delta, -Tplot)); v = (Tplot.^delta) .* gamma(1-delta) .* gamma(1+delta); % Initialize result matrices p_d = zeros(length(lambda_d), length(Tplot)); p_c = zeros(length(lambda_d), length(Tplot)); for k = 1:length(lambda_d) p_d(k,:) = lambda_d(k) ./ ((lambda_d(k) .* (v + 1) + lambda_c .* (P_c/P_d).^delta) .* v); p_c(k,:) = lambda_c ./ ((lambda_c*(mu + 1) + lambda_d(k).*P_d/P_c).^delta) .* v; end % Plot with labels, legend, and log scale for readability figure; plot(Tplot, p_c, 'LineWidth', 1.5); hold on; plot(Tplot, p_d, 'LineWidth', 1.5); xlabel('T'); ylabel('Probability'); legend({'p_c (λ_d=0.002)','p_c (λ_d=0.01)','p_c (λ_d=0.05)','p_d (λ_d=0.002)','p_d (λ_d=0.01)','p_d (λ_d=0.05)'}, 'Location', 'best'); xscale('log'); % Match x-axis to log-spaced Tplot grid on;
Quick Notes
- Ensure you have the Symbolic Math Toolbox installed to use
hypergeom. If you don't have it, you'll need a numerical implementation of the general hypergeometric function_2F1. - Adding
xscale('log')makes the plot easier to interpret since your x-axis values are on a logarithmic range.
内容的提问来源于stack exchange,提问作者Abdulhameed

