MATLAB Answers

Why is the graph linear instead of curve?

11 ビュー (過去 30 日間)
Arturo Escalante Castillo
Arturo Escalante Castillo 2021 年 4 月 12 日 5:52
回答済み: KSSV 2021 年 4 月 12 日 6:18
x=[0 8 16 25 37];
y=[5 17 12 21 19];
disp("Los datos del largo de la pieza son:")
x
disp('Los datos del ancho de la pieza son:')
y
n=length(x)-1;
xo=19;
disp("El orden de este polinomio es de: "+n)
%Diferencias divididas%
m1=(y(2)-y(1))/(x(2)-x(1));
m2=(y(3)-y(2))/(x(3)-x(2));
m3=(y(4)-y(3))/(x(4)-x(3));
m4=(y(5)-y(4))/(x(5)-x(4));
disp('')
%%%%%%%%%%%%%%%%%%%%%%%
b0=y(1);
b1=m1;
b2=(m2-b1)/(x(3)-x(1));
b3=(((m3-m2)/(x(4)-x(2))-b2)/(x(4)-x(1)));
b4=(((m4-m3)/(x(5)-x(3))-((m3-m2)/(x(4)-x(2))))/(x(5)-x(2))-(((m3-m2)/(x(4)-x(2)))-((m2-m1)/(x(3)-x(1))))/(x(4)-x(1)))/(x(5)-x(1));
fn= b0+b1*(xo-x(1))+b2*(xo-x(1))*(xo-x(2))+b3*(xo-x(1))*(xo-x(2))*(xo-x(3))+b4*(xo-x(1))*(xo-x(2))*(xo-x(3))*(xo-x(4));
disp("El resultado del polinomio es: "+fn);
plot(x,y,'p')
f4=@(x) 5+1.5*(x-0)-0.13281*(x-0).*(x-8)+0.009136*(x-0).*(x-8).*(x-16)-0.00038778*(x-0).*(x-8).*(x-16).*(x-25);
g=min(x)-3:1:max(x)+3;
hold
plot(x,f4(x))
grid

回答 (1 件)

KSSV
KSSV 2021 年 4 月 12 日 6:18
You have to increase the number of values in x, when calling the function.
x=[0 8 16 25 37];
y=[5 17 12 21 19];
disp("Los datos del largo de la pieza son:")
Los datos del largo de la pieza son:
x
x = 1×5
0 8 16 25 37
disp('Los datos del ancho de la pieza son:')
Los datos del ancho de la pieza son:
y
y = 1×5
5 17 12 21 19
n=length(x)-1;
xo=19;
disp("El orden de este polinomio es de: "+n)
El orden de este polinomio es de: 4
%Diferencias divididas%
m1=(y(2)-y(1))/(x(2)-x(1));
m2=(y(3)-y(2))/(x(3)-x(2));
m3=(y(4)-y(3))/(x(4)-x(3));
m4=(y(5)-y(4))/(x(5)-x(4));
disp('')
%%%%%%%%%%%%%%%%%%%%%%%
b0=y(1);
b1=m1;
b2=(m2-b1)/(x(3)-x(1));
b3=(((m3-m2)/(x(4)-x(2))-b2)/(x(4)-x(1)));
b4=(((m4-m3)/(x(5)-x(3))-((m3-m2)/(x(4)-x(2))))/(x(5)-x(2))-(((m3-m2)/(x(4)-x(2)))-((m2-m1)/(x(3)-x(1))))/(x(4)-x(1)))/(x(5)-x(1));
fn= b0+b1*(xo-x(1))+b2*(xo-x(1))*(xo-x(2))+b3*(xo-x(1))*(xo-x(2))*(xo-x(3))+b4*(xo-x(1))*(xo-x(2))*(xo-x(3))*(xo-x(4));
disp("El resultado del polinomio es: "+fn);
El resultado del polinomio es: 12.9293
plot(x,y,'p')
x = linspace(min(x),max(x)) ;
f4=@(x) 5+1.5*(x-0)-0.13281*(x-0).*(x-8)+0.009136*(x-0).*(x-8).*(x-16)-0.00038778*(x-0).*(x-8).*(x-16).*(x-25);
g=min(x)-3:1:max(x)+3;
hold
Current plot held
plot(x,f4(x))
grid

タグ

製品


リリース

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by