Matlab - Ayuda: Graficar splines cúbicos

 
Vista:

Ayuda: Graficar splines cúbicos

Publicado por Fernando Montiel (1 intervención) el 25/04/2019 02:47:52
Mi programa genera la matriz de los datos y obtiene los coeficientes más no he logrado tener un gráfica de los polinomios generados. Les comparto el código.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
%Datos
x = [-1; 1; 2; 5];
y = [-1; 1; 5; 3];
 
%%%%%%%%% 3.- Splines cúbicos %%%%%%%%%%%%
 
N = length(y); %Número de datos
Num_eq = N - 1; %Número de ecuaciones
coef = 4 * Num_eq; %Coeficientes
Num_filas = 2 * (N - 2) + 2;
BY = zeros(2*(N-2)+2 + (N-2) + (N-2) + 2,1); %Vector B
 
H0 = zeros(Num_filas, coef); % H0 -> Matriz de las ec.
%Orden por grado x^3, x^2, x ...
for idx = 1:(Num_filas/2)
    col = (idx-1)*4+1;
    fila = (idx-1)*2+1;
    for jdx = col:(col+3)
        H0(fila,jdx) = x(idx)^(3 - jdx + col);
        H0(fila+1,jdx) = x(idx+1)^(3 - jdx + col);
    end
    BY(fila,1) = y(idx);
    BY(fila+1,1) = y(idx+1);
end
 
H1 = zeros(N - 2, coef); %H1 -> Matriz primera derivada
for idx = 2:(N - 1)
    col = 1 + (idx - 2)*4;
    H1(idx - 1, col) = 3*x(idx)^2;
    H1(idx - 1, col + 1) = 2*x(idx);
    H1(idx - 1, col + 2) = 1;
    H1(idx - 1, col + 3) = 0;
    H1(idx - 1, col + 4) = -3*x(idx)^2;
    H1(idx - 1, col + 5) = -2*x(idx);
    H1(idx - 1, col + 6) = -1;
end
 
H2 = 0*H1; %H2 -> Matriz segunda derivada
for idx = 2:(N - 1)
    col = 1 + (idx - 2)*4;
    H2(idx - 1, col) = 6*x(idx);
    H2(idx - 1, col + 1) = 2;
    H2(idx - 1, col + 2) = 0;
    H2(idx - 1, col + 3) = 0;
    H2(idx - 1, col + 4) = -6*x(idx);
    H2(idx - 1, col + 5) = -2;
    H2(idx - 1, col + 6) = 0;
end
 
H3 = zeros(2, coef); %H3 -> Matriz de extremos
H3(1,1) = 6*x(1,1);
H3(1,2) = 2;
H3(2,end-2) = 2;
H3(2,end-3) = 6*x(end);
 
%Revisar H3
H = [H0;H1;H2;H3];%Matriz
 
%Solución a coeficientes
X = H\BY;
 
%Gráfica
L = 1;
U = 1;
kx = size(x,1);%Tamaño datos x
KX = size(X,1);%Tamaño vector coef
while L < KX
    syms s;
    d = X(L,1) * s.^3;
    L = L +1;
    c = X(L,1) * s.^2;
    L = L +1;
    b = X(L,1) * s;
    L = L+1;
    a = X(L,1);
    L = L+1;
    funcion = d + c + b + a;%Polinomio
        %Gráfica del polinomio
        while U < kx
        di = x(U,1);
        din = x(U+1,1);
        dom = [di din];
        fplot(funcion,dom);
        U = U+1;
        grid on
        end
end
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de JOSE JEREMIAS CABALLERO
Val: 6.975
Oro
Ha mantenido su posición en Matlab (en relación al último mes)
Gráfica de Matlab

Ayuda: Graficar splines cúbicos

Publicado por JOSE JEREMIAS CABALLERO (5917 intervenciones) el 04/09/2019 15:27:25
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
clear
%Datos
x = [-1; 1; 2; 5];
y = [-1; 1; 5; 3];
 
%%%%%%%%% 3.- Splines cúbicos %%%%%%%%%%%%
 
N = length(y); %Número de datos
Num_eq = N - 1; %Número de ecuaciones
coef = 4 * Num_eq; %Coeficientes
Num_filas = 2 * (N - 2) + 2;
BY = zeros(2*(N-2)+2 + (N-2) + (N-2) + 2,1); %Vector B
 
H0 = zeros(Num_filas, coef); % H0 -> Matriz de las ec.
%Orden por grado x^3, x^2, x ...
for idx = 1:(Num_filas/2)
    col = (idx-1)*4+1;
    fila = (idx-1)*2+1;
    for jdx = col:(col+3)
        H0(fila,jdx) = x(idx)^(3 - jdx + col);
        H0(fila+1,jdx) = x(idx+1)^(3 - jdx + col);
    end
    BY(fila,1) = y(idx);
    BY(fila+1,1) = y(idx+1);
end
 
H1 = zeros(N - 2, coef); %H1 -> Matriz primera derivada
for idx = 2:(N - 1)
    col = 1 + (idx - 2)*4;
    H1(idx - 1, col) = 3*x(idx)^2;
    H1(idx - 1, col + 1) = 2*x(idx);
    H1(idx - 1, col + 2) = 1;
    H1(idx - 1, col + 3) = 0;
    H1(idx - 1, col + 4) = -3*x(idx)^2;
    H1(idx - 1, col + 5) = -2*x(idx);
    H1(idx - 1, col + 6) = -1;
end
 
H2 = 0*H1; %H2 -> Matriz segunda derivada
for idx = 2:(N - 1)
    col = 1 + (idx - 2)*4;
    H2(idx - 1, col) = 6*x(idx);
    H2(idx - 1, col + 1) = 2;
    H2(idx - 1, col + 2) = 0;
    H2(idx - 1, col + 3) = 0;
    H2(idx - 1, col + 4) = -6*x(idx);
    H2(idx - 1, col + 5) = -2;
    H2(idx - 1, col + 6) = 0;
end
 
H3 = zeros(2, coef); %H3 -> Matriz de extremos
H3(1,1) = 6*x(1,1);
H3(1,2) = 2;
H3(2,end-2) = 2;
H3(2,end-3) = 6*x(end);
 
%Revisar H3
H = [H0;H1;H2;H3];%Matriz
 
%Solución a coeficientes
X = H\BY;
 
%Gráfica
L = 1;
U = 1;
kx = size(x,1);%Tamaño datos x
KX = size(X,1);%Tamaño vector coef
figure(gcf)
hold on
while L < KX
    syms s;
    d = X(L,1) * s.^3;
    L = L +1;
    c = X(L,1) * s.^2;
    L = L +1;
    b = X(L,1) * s;
    L = L+1;
    a = X(L,1);
    L = L+1;
    funcion = d + c + b + a; %Polinomio
    f=inline(funcion);
 
        %Gráfica del polinomio
        while U < kx
        di = x(U,1);
        din = x(U+1,1);
        dom = [di din];
        fplot(funcion,dom,'linewidth',2);
        xlim([min(x)-0.5, max(x)+0.5])
        ylim([min(f(x))-1 max(f(x))+1])
        U = U+1;
        grid on
        pause(1)
        end
end
hold off


Saludos
JOSE JEREMIAS CABALLERO
Asesor de Proyectos con Matlab
Servicios de programación matlab
Asesoría Online en Matlab


http://matlabcaballero.blogspot.com
https://www.facebook.com/matlabcaballero
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar