我无法弄清楚为什么我的循环不在matlab中

发布于 2025-01-26 15:37:47 字数 1194 浏览 5 评论 0原文

在MATLAB中,我试图编写一个程序,该程序将在图(x,y)上使用3个坐标,使用这些值来求解一个方程系统,该方程将找到多项式方程的系数,y = ax^2 + BX + C,然后我可以用来绘制抛物线。

为了测试我的代码,我认为我可以从多项式开始,绘制图形,找到抛物线的最小位置,将其直接邻居用于我的其他两个位置,然后通过我的代码运行这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')

“

In MATLAB, I am trying to write a program that will take 3 coordinates on a graph, (x,y), use those values to solve a system of equations that will find the coefficients of a polynomial equation, y = ax^2 + bx + c, which I can then use to plot a parabola.

To test my code, I figured I could start with a polynomial, graph it, find the minimum location of the parabola, use its immediate neighbors for my other 2 locations, then run those 3 locations through my code which should spit out the coefficients of my original polynomial. But for some reason, my resulting parabola is right shifted and my values for b and c are incorrect.

Does anyone see where my issue is? I am out of ideas

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')

plot

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

血之狂魔 2025-02-02 15:37:47

您将索引混淆到您的数组y和相应的x坐标。

x = -10:10;
y = 6.*x.^2 + 11.*x -35;

[max_n, max_i] = min(y)

这里。 max_iy数组中的索引,相应的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)pts_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]);

You are confusing the index into your array y, and the corresponding x coordinate.

x = -10:10;
y = 6.*x.^2 + 11.*x -35;

[max_n, max_i] = min(y)

Here. max_i is the index into the y array, the corresponding x coordinate would be x(max_i).

I suggest you find three data points to fit your curve to as follows:

[~, max_i] = min(y);
pts_x = x(max_i + (-1:1));
pts_y = y(max_i + (-1:1));

then use pts_x(i) and pts_y(i) as your x and y values:

syms a b c
equ = a * pts_x.^2 + b * pts_x + c == pts_y;
sol = solve(equ, [a, b, c]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文