Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
1.6 kB
1
Indexable
Never
clear;
clc;

n = 8;
xw = [-1.1, -0.83, -0.57, -0.28, -0.02, 0.16, 0.47, 0.95, 1.23];
m = 2;
// m = 0, 1, 2, 33
lp = 125;

function y = f(x);
    y = exp(sin(x));
endfunction;

for i=0:n;
    yw(i+1) = f(xw(i+1));
end;

for j=0:(2*m);
    suma = 0;
    for i=0:n;
        suma = suma + (xw(i+1))^j;    
    end;
    g(j+1) = suma;
end;

for j=0:m;
    suma = 0;
    for i=0:n;
        suma = suma + yw(i+1) * (xw(i+1))^j;
    end;
    r(j+1) = suma;
end;

select m
case 0 then
    for i=0:n;
        a(1) = r(1)/g(1);
    end;
case 1 then
    l1=-g(3)*r(1) + g(2)*r(2);
    l2=g(2)*r(1) + g(1)*r(2);
    d=g(2)*g(2)-g(1)*g(3);
    a(1)=l1/d;
    a(2)=l2/d;
case 2 then
    l1 = g(4)*g(4)*r(1) - g(3)*g(5)*r(1) + g(2)*g(5)*r(2) + g(3)*g(3)*r(3) - g(4)*(g(3)*r(2)+g(2)*r(3));
    
    l2 = g(2)*g(5)*r(1) + g(3)*g(3)*r(2) - g(3)*(g(3)*r(1) + g(2)*r(3)) + g(1)*(-g(5)*r(2) + g(4)*r(3));
    
    l3 = g(3)*g(3)+r(1) - g(2)*g(4)*r(1) + g(1)*g(4)*r(2) + g(2)*g(2)*g(3) - g(3)*(g(2)*r(2) + g(1)*r(3));
    
    d=(g(3)^3) + g(1)*g(4)*g(4) + g(2)*g(2)*g(5) - g(3)*(2*g(2)*g(4) + g(1)*g(5));
    
    a(1)=l1/d;
    a(2)=l2/d;
    a(3)=l3/d;
    else
        disp("Nieprawidłowa wartość parametru m");
end;

function y=F(x, m, a);
    y = 0;
    for j=0:m;
        y= y+a(j+1) * x^j;
    end;
endfunction;

if m==0 || m==1 || m==2 then
    xt = linspace(xw(1)-0.1,xw(n+1)+0.1,lp)';
    for i=1:lp;
        Ft(i) = F(xt(i),m,a);
    end;
    plot(xt, Ft);
    plot(xw,yw,".k");
end;
Leave a Comment