-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcomparar_errores.m
402 lines (311 loc) · 11.2 KB
/
comparar_errores.m
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
function comparar_errores(matriz,decimales)
decimales = str2double(decimales)
%prueba
% Create figure
h.f = figure('Name','Seleccione los modelos de aproximacion:','units','pixels','position',[200,200,250,300],...
'toolbar','none','menu','none');
% Create yes/no checkboxes
h.c(1) = uicontrol('style','checkbox','units','pixels',...
'position',[10,30,200,15],'string','1) Modelo Lineal');
h.c(2) = uicontrol('style','checkbox','units','pixels',...
'position',[10,60,300,25],'string','2) Modelo Parabolico');
h.c(3) = uicontrol('style','checkbox','units','pixels',...
'position',[10,90,400,35],'string','3) Modelo Exponencial');
h.c(4) = uicontrol('style','checkbox','units','pixels',...
'position',[10,120,500,45],'string','4) Modelo Potencial');
h.c(5) = uicontrol('style','checkbox','units','pixels',...
'position',[10,150,600,55],'string','5) Modelo Hiperbolico');
% Create OK pushbutton
h.p = uicontrol('style','pushbutton','units','pixels',...
'position',[40,200,200,20],'string','OK',...
'callback',@p_call);
% Pushbutton callback
function p_call(varargin)
vals = get(h.c,'Value');
disp(vals);
checked = find([vals{:}]);
if isempty(checked)
checked = 'none';
end
if (~isempty(checked))
disp(checked);
close(h.f);
comparar_errores2(matriz,decimales,checked);
end
end
end
%end
%endprueba
function comparar_errores2(matriz,decimales,checked)
sumaerror = [0];
for a = 2:length(checked)
sumaerror = [sumaerror , 0];
end
tabla = [];
listax = devolverx(matriz);
listay = devolvery(matriz);
name = ["1) Error lineal ","2) Error Parabolico","3) Error Exponencial","4) Error Potencial","5) Error Hiperbolico"];
nombre = ["xi","yi"]
for con = 1:length(checked)
nombre = [nombre, name(checked(con))];
end
for i = 1:length(listax)
% resolverEcMinimosCuadrados(matriz,tipo_de_aproximacion,decimales)
%(matriz,intervalo,decimales,tipo)
aux = [listax(i),listay(i)];
disp("seleccionados: ");
disp(checked);
for ii = 1:length(checked)
error = ylineal( resolverEcMinimosCuadrados(matriz,checked(ii),decimales),listax(i),decimales,checked(ii));
error = listay(i) - error;
error = error ^2;
error = round(error,decimales);
aux = [aux ,error];
sumaerror(ii) = sumaerror(ii) + error;
end
tabla = [tabla;aux];
end
tabla = [tabla ; 0,0, sumaerror];
resultado = compararErrores(sumaerror);
estring = ["El error cuadratico minimo lo tiene el modelo" mat2str(checked(resultado))];
graficar_matriz3(tabla,nombre,estring);
%graficar_colo(coeficientes,flagPuntos,puntos,tipoFuncion,inter,decimales)
graficar_colo(resolverEcMinimosCuadrados(matriz,checked(resultado),decimales),1,matriz,checked(resultado),intervalo(matriz),decimales);
end
function ret = compararErrores(vector)
ret = 0;
for i = 1:length(vector)
if min(vector) == vector(i)
ret = i;
end
end
return
end
function graficar_matriz3(algo,nombre,estring)
%
pantallaTabla = figure('Name','Tabla de comparacion de errores','MenuBar','none');
%set(pantallaTabla,'Units','normalized');
t = uitable(pantallaTabla,'Data',algo,'Position',[0 200 800 200]);
t.ColumnName = nombre;
texto = uicontrol('parent',pantallaTabla,'Position',[10,10,400,100],'Style','Text','String',estring,'ForegroundColor',[0 0 0],'BackgroundColor', [.94 .94 .94]);
end
%yLineal recibe una matriz y 'x' y devuelve la funcion especificada en ese valor.
function ret = ylineal(matriz,intervalo,decimales,tipo)
coeficienteA = matriz(1);
coeficienteB = matriz(2);
if tipo == 2
coeficienteC = matriz(3);
end
switch tipo
case 1 %aproximacion por recta de cuadrados minimos
ret= (coeficienteA*(intervalo)) + coeficienteB;
case 2 %aproximacion por parabola de cuadrados minimos
ret =coeficienteA * ((intervalo).^2) + coeficienteB * (intervalo) + coeficienteC;
case 3 %aproximacion exponencial
ret = (exp(coeficienteB)*(exp(coeficienteA * intervalo)));
case 4 %aproximacion potencial
ret = ((exp( coeficienteB)) *(intervalo.^ coeficienteA ));
case 5 %aproxima hiperbola
b = coeficienteA / coeficienteB;
a = 1/coeficienteB;
a = round (a,decimales);
b= round(b,decimales);
ret = ( a ./ (b + intervalo' ));
end
ret = round(ret,decimales);
return
end
%Esta funcion recibe una matriz y un tipo de aproximacion y devuleve los valores, a , b y c de la funcion;
%Si mostrar = 1 => muestra la funcion aproximante ; si mostrar = 0 => obtiene detalle del calculo
%si mostrar = 2 => muestra la funcion y los puntos
%recibe una matriz de la manera ([X] [Y]) y devuelve un vector con los valores a y b
function ret = resolverEcMinimosCuadrados(matriz,tipo_de_aproximacion,decimales)
x = devolverx(matriz);
y = devolvery(matriz);
switch tipo_de_aproximacion
case 1
matriz_A = [round(sumaCuadrados(x),decimales),round(sumaVector(x),decimales);round(sumaVector(x),decimales),length(y)];
matriz_B = [round(sumaVector(multiplicacionXY(matriz)),decimales);round(sumaVector(y),decimales)];
ret = inv(matriz_A)* matriz_B;
case 2 %Parábola de mÃnimos cuadrados: ð?‘¦ = ð?‘Žð?‘¥^2+ ð?‘?ð?‘¥ + ð?‘?
matriz_A = [round(sumaCuarta(devolverx(matriz)),decimales),sumaCubo(devolverx(matriz)),sumaCuadrados(devolverx(matriz));
sumaCubo(devolverx(matriz)),sumaCuadrados(devolverx(matriz)),sumaVector(devolverx(matriz));
sumaCuadrados(devolverx(matriz)),sumaVector(devolverx(matriz)),length(devolverx(matriz))];
matriz_B = [sumaVector(multiplicacionXXY(matriz));sumaVector(multiplicacionXY(matriz));sumaVector(devolvery(matriz))];
ret = inv(matriz_A)* matriz_B;
case 3 %caso exponencial
aux = devolverx(matriz).*(log(devolvery(matriz)));
ylog = log(devolvery(matriz));
matriz_A = [sumaCuadrados(devolverx(matriz)),sumaVector(devolverx(matriz));sumaVector(devolverx(matriz)),length(devolverx(matriz))]
matriz_B = [ sumaVector(aux) ;sumaVector(ylog)]
ret = inv(matriz_A)* matriz_B
case 4 %caso potencial
ylog = log(devolvery(matriz));
xlog = log(devolverx(matriz));
aux = xlog.*ylog;
matriz_A = [sumaCuadrados(xlog),sumaVector(xlog);sumaVector(xlog),length(devolverx(matriz))]
matriz_B = [ sumaVector(aux) ;sumaVector(ylog)]
ret = inv(matriz_A)* matriz_B
case 5 %caso hiperbola
aux = hiperbolaxy(matriz);
auxy = hiperbolay(devolvery(matriz));
matriz_A = [sumaCuadrados(devolverx(matriz)),sumaVector(devolverx(matriz));sumaVector(devolverx(matriz)),length(devolverx(matriz))]
matriz_B = [ sumaVector(aux) ;sumaVector(auxy)];
ret = inv(matriz_A)* matriz_B
end
ret = round(ret,decimales);
return
end
%Tuve que hacer esta funcion para encontrar la solucion a un error
function ret = elevar(i)
var = 0;
z=1;
disp(i);
disp(str2num(i));
if i > 0
var = 10;
while (z < str2num(i))
var = var *10;
z=z+1;
end
else
var = 1;
end
ret = var;
return
end
function retorno = error1aprox(matriz,aprox,decimales,tipo)
retorno = 0;
matrizy= devolvery(matriz);
matrizx=devolverx(matriz);
for i = 1:length(matriz);
retorno = retorno + ylineal(aprox,matrizx(i),decimales,tipo) - matrizy(i);
end
retorno = round(retorno,decimales);
return
end
function retorno = error2aprox(matriz,aprox,decimales,tipo)
retorno = 0;
matrizy= devolvery(matriz);
matrizx=devolverx(matriz);
for i = 1:length(matriz);
retorno = retorno + (ylineal(aprox,matrizx(i),decimales,tipo) - matrizy(i)) ^ 2 ;
end
retorno = round(retorno ,decimales);
return
end
%sumaVector recibe un vector y devuelve un numero = la suma de sus valores
function numero = sumaVector(vector)
var = 0;
for i = 1:length(vector)
var = var + vector(i);
end
numero = var;
return
end
%sumaCuadrados recibe un vector y devuelve un numero = la suma de los valores x^2
function numero = sumaCuadrados(vector)
var = 0;
for i = 1:length(vector)
var = var + vector(i) * vector(i);
end
numero = var;
return
end
function var = hiperbolay(vector)
var = [];
for i = 1:length(vector)
if (vector(i) ~= 0)
var = [var , 1 / vector(i)];
end
end
return
end
function var = hiperbolaxy(vector)
x = devolverx(vector);
y = devolvery(vector);
yinv = hiperbolay(y);
var = [];
for i = 1:length(vector)
if (vector(i) ~= 0)
var = [var ; (x(i)*yinv(i))];
end
end
return
end
function numero = sumaCubo(vector)
var = 0;
for i = 1:length(vector)
var = var + vector(i) * vector(i) * vector(i);
end
numero = var;
return
end
function numero = sumaCuarta(vector)
var = 0;
for i = 1:length(vector)
var = var + vector(i) * vector(i) * vector(i) * vector(i);
end
numero = var;
return
end
%sumax recime una matriz de la manera [[x] [y]] y devuelve la suma de los valores x
function numero = sumax(matriz_sumaX)
matriz_sumaX = transpose( matriz_sumaX);
x = matriz_sumaX(1,:);
numero = sumaVector(x);
return
end
%sumay recime una matriz de la manera [[x] [y]] y devuelve la suma de los valores x
function numero = sumay(matriz_sumay)
matriz_sumay = devolvery(matriz_sumay);
numero = sumaVector(y);
return
end
%funcion devolvery recibe una matriz de la manera [[x] [y]] y devuelve un vector con los valores y
function y = devolvery(matris)
matris = transpose( matris);
y = matris(2,:);
return
end
%funcion devolverx recibe una matriz de la manera [[x] [y]] y devuelve un vector con los valores x
function x = devolverx(matris)
matris = transpose( matris);
x = matris(1,:);
return
end
%recibe una matriz [[X] [Y]] y devuelve un numero = suma de ( X * Y)
function returnValue = multiplicacionXY (matrix)
x = devolverx(matrix) ;
y = devolvery(matrix) ;
respu = x.*y ;
%Devuelve una fila con la multiplicacion
returnValue = sumaVector(respu);
return
end
%recibe una matriz [[X] [Y]] y devuelve un numero = suma de ( X * Y)
function returnValue = multiplicacionXXY (matrix)
x = devolverx(matrix) ;
y = devolvery(matrix) ;
respu = x.*x.*y ;
%Devuelve una fila con la multiplicacion
returnValue = sumaVector(respu);
return
end
%devuelve la mayorCoordenada del eje x , va a servir para graficar
function retorno = mayorCoordenadaX(matrix)
x = devolverx(matrix);
retorno = max(x);
return
end
%devuelve la menorCoordenada del eje x , va a servir para graficar
function retorno = menorCoordenadaX(matrix)
x = devolverx(matrix);
retorno = min(x);
return
end
function retorno = intervalo(matrix)
x = devolverx(matrix);
retorno = [min(x),max(x)];
return
end