You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何在MATLAB中求解并绘制三次方程x³-dx²+1=0的虚实根

Hey there, fellow MATLAB newbie! Let's break down how to solve and visualize the roots of your cubic equation (x^3 - d x^2 + 1 = 0) step by step. I'll keep things clear with actual code and explanations so you can follow along easily.

Solving & Visualizing Roots of (x^3 - d x^2 + 1 = 0)

First, let's recap what we're dealing with: we need to find all roots (real and complex) for (d) ranging from -10 to 10, and visualize how these roots behave, with (x) constrained to [-5,5].

1. Core Approach

MATLAB has a built-in roots() function that makes solving polynomial equations a breeze. For each value of (d), our cubic equation has coefficients [1, -d, 0, 1] (since the (x) term has a coefficient of 0). We'll loop through each (d), compute the roots, separate real and complex ones, then plot them.

2. Full MATLAB Code

Here's a complete, commented script you can copy-paste and run directly:

% Generate a smooth range of d values (200 points between -10 and 10)
d_values = linspace(-10, 10, 200);

% Initialize arrays to store root data
real_roots = [];
complex_real_parts = [];
complex_imag_parts = [];

% Iterate over each d to solve the equation
for d = d_values
    % Define the polynomial coefficients: x³ - d x² + 0x + 1 = 0
    coeffs = [1, -d, 0, 1];
    % Calculate all roots (real and complex)
    roots_x = roots(coeffs);
    
    % Separate real roots (account for floating-point errors with a small threshold)
    is_real = abs(imag(roots_x)) < 1e-10;
    current_real_roots = real(roots_x(is_real));
    current_complex_roots = roots_x(~is_real);
    
    % Store real roots (could be 1 or 3 per d)
    real_roots = [real_roots, current_real_roots];
    
    % Store complex root data (conjugate pairs, so we only need one's imaginary part)
    if ~isempty(current_complex_roots)
        complex_real_parts = [complex_real_parts, real(current_complex_roots(1))];
        complex_imag_parts = [complex_imag_parts, abs(imag(current_complex_roots(1)))];
    end
end

% --- Plot 1: Real Roots vs d ---
figure('Name','Real Roots of Cubic Equation');
hold on; grid on;

% Plot each real root for every d (handles 1 or 3 roots per d)
root_index = 1;
for i = 1:length(d_values)
    % Count how many real roots this d has
    num_real = sum(abs(imag(roots([1,-d_values(i),0,1]))) < 1e-10);
    % Plot the real roots as blue dots
    plot(d_values(i), real_roots(root_index:root_index+num_real-1), 'bo', 'MarkerSize',4);
    root_index = root_index + num_real;
end

% Add labels and constraints
xlabel('Parameter d');
ylabel('Real Root x');
title('Real Roots of \(x^3 - d x^2 + 1 = 0\) (d ∈ [-10,10], x ∈ [-5,5])');
xlim([-10,10]);
ylim([-5,5]); % Enforce your requested x range
hold off;

% --- Plot 2: Complex Roots vs d ---
figure('Name','Complex Roots of Cubic Equation');
hold on; grid on;

% Plot real part of complex roots (red solid line)
plot(d_values, complex_real_parts, 'r-', 'LineWidth',1.5);
% Plot positive and negative imaginary parts (blue dashed lines)
plot(d_values, complex_imag_parts, 'b--', 'LineWidth',1.5);
plot(d_values, -complex_imag_parts, 'b--', 'LineWidth',1.5);

% Add labels and constraints
xlabel('Parameter d');
ylabel('Real/Imaginary Part of Complex Root');
title('Complex Roots of \(x^3 - d x^2 + 1 = 0\) (d ∈ [-10,10], x ∈ [-5,5])');
legend('Real Part of Complex Roots','Positive Imaginary Part','Negative Imaginary Part');
xlim([-10,10]);
ylim([-5,5]);
hold off;

3. Key Explanations

  • Floating-Point Threshold: We use abs(imag(roots_x)) < 1e-10 instead of isreal() because MATLAB might return tiny imaginary parts for roots that are mathematically real (due to calculation errors).
  • Real Root Behavior: You'll notice that for some (d) values, there are 3 real roots, and for others only 1—this is normal for cubic equations (they always have at least one real root).
  • Complex Roots: Complex roots come in conjugate pairs, so their imaginary parts are opposites—hence we only need to compute and plot one, then mirror it for the negative value.

4. Quick Tweaks

  • If you want smoother curves, increase the number of points in linspace() (e.g., linspace(-10,10,500)).
  • To adjust the x range, just modify the ylim() values in both plots.

内容的提问来源于stack exchange,提问作者john

火山引擎 最新活动