在 MATLAB 中,我正在嘗試撰寫一個程式,該程式將在圖形上采用 3 個坐標 (x,y),使用這些值來求解方程組,該方程組將找到多項式方程的系數,y = ax^2 bx c,然后我可以用它來繪制拋物線。
為了測驗我的代碼,我想我可以從一個多項式開始,繪制它,找到拋物線的最小位置,將它的直接鄰居用于我的其他 2 個位置,然后通過我的代碼運行這 3 個位置,這應該會吐出系數我原來的多項式。但由于某種原因,我得到的拋物線右移,我的 b 和 c 值不正確。
有誰看到我的問題在哪里?我沒有想法
clear all; close all;
x = -10:10;
%Original Polynomial
y = 6.*x.^2 11.*x -35;
% Find 3 Locations
[max_n, max_i] = min(y)
max_il = max_i - 1 % left neighbor of max_ni
max_nl = y(max_il) % value at max_il
max_ir = max_i 1 % left neighbor of max_ni
max_nr = y(max_ir) % value at max_ir
% Solve for coefficients
syms a b c
equ = (a)*(max_i)^2 (b)*(max_i) (c) == (max_n);
equ_l = (a)*(max_il)^2 (b)*(max_il) (c) == (max_nl);
equ_r = (a)*(max_ir)^2 (b)*(max_ir) (c) == (max_nr);
sol = solve([equ, equ_l, equ_r],[a, b, c]);
Sola = sol.a
Solb = sol.b
Solc = sol.c
% New Polynomial
p = (sol.a).*(x).^2 (sol.b).*(x) (sol.c);
%Plot
plot(x,y); grid on; hold on;
plot(x, p);
axis([-10 10 -41 40])
[max_np, max_ip] = min(p)
legend('OG', 'New')

uj5u.com熱心網友回復:
您將索引與陣列混淆y,以及相應的 x 坐標。
x = -10:10;
y = 6.*x.^2 11.*x -35;
[max_n, max_i] = min(y)
這里。max_i是y陣列的索引,對應的 x 坐標為x(max_i).
我建議你找到三個資料點來擬合你的曲線,如下所示:
[~, max_i] = min(y);
pts_x = x(max_i (-1:1));
pts_y = y(max_i (-1:1));
然后使用pts_x(i)andpts_y(i)作為你的 x 和 y 值:
syms a b c
equ = a * pts_x.^2 b * pts_x c == pts_y;
sol = solve(equ, [a, b, c]);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/475420.html
