Matlab - Ayuda con este codigo

 
Vista:
sin imagen de perfil
Val: 13
Ha aumentado su posición en 7 puestos en Matlab (en relación al último mes)
Gráfica de Matlab

Ayuda con este codigo

Publicado por Mauricio (5 intervenciones) el 16/11/2020 18:22:15
Hola amigos programadores tengo un problema tengo este codigo que quiero hacerle funcionar el codigo es para: se ingresa unos datos para determinar un costo asi como en las imagenes, trato de hacerlo funcionar pero el problema no arranca me aparece errores que no les entiendo muy ya que estoy incursionando poco a poco al manejo de matlab, tambien otra duda el codigo que les indico fue hecho en la version 2011 o 2010 a vista, y yo lo cree tal cual pero en la version 2019 agradeceria que me iluminaran donde es mi fallo



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
clc;
Pr= input('Ingrese la potencia requerida en Kw = 40000');
lambda(1) = input('Ingrese el valor de lambda en ctvs$ = 8.0 ');
bandera = 0;
flag=0;
 
%Valores de Potencias Mínimas y Máximas de las unidades Base
pbmax(1) = 9900;
pbmin(1) = 3600;
pbmax(2) = 9900;
pbmin(2) = 3600;
pbmax(3) = 9900;
pbmin(3) = 3600;
pbmax(4) = 9900;
pbmin(4) = 3600;
pbmax(5) = 9900;
pbmin(5) = 3600;
pbmax(6) = 9900;
pbmin(6) = 3600;
 
Potbmax = 0;
Potbmin = 0;
 
for i=1:6
    Potbmax = Potbmax + pbmax(i);
    Potbmin = Potbmin + pbmin(i);
end
 
if Pr<=Potbmin
    bandera = 1;
end
 
if Pr>Potbmin && Pr<=Potbmax
    ecuaciones = [-1.803474E-12 6.807022E-08 -8.42656E-04 1.146129E+01;...
             -1.6233693E-12 6.2586799E-08 -7.9518109E-04 1.1249170E+01;...
             -1.6233693E-12 6.2586799E-08 -7.9518109E-04 1.1249170E+01;...
             -1.7043272E-12 6.5084006E-08 -8.1695508E-04 1.1359717E+01;...
             -1.4268208E-12 5.8170640E-09 -7.7182460E-04 1.1291629E+01;...
             -1.7309252E-12 6.6377504E-08 -8.3832297E-04 1.1450687E+01];
 
%1ra iteración
total = 0;
resultado(1,1) = 1;
resultado(1,2) = lambda(1);
resultado(1,3) = 0;
[s, t] = size(ecuaciones);
for i=1:s
    for j=1:t
        ecuacion(j) = ecuaciones(i,j);
    end
    resultado(1,i+3) = raices(ecuacion, lambda(1), pbmax(i), pbmin(i));
    total = total + resultado(1,i+3);
end
resultado(1,3) = total;
error(1) = total - Pr;
 
%2da iteración
if total>Pr
    lambda(2) = 1.1*lambda(1);
else
    lambda(2) = 0.9*lambda(1);
end
total = 0;
resultado(2,1) = 2;
resultado(2,2) = lambda(2);
resultado(2,3) = 0;
for i=1:s
    for j=1:t
        ecuacion(j) = ecuaciones(i,j);
    end
    resultado(2,i+3) = raices(ecuacion,lambda(2),pbmax(2),pbmax(i),pbmin(i));
    total = total + resultado(2,i+3);
end
resultado(2,3) = total;
error(2) = total - Pr;
 
%siguientes iteraciones
e = 3;
total = 0;
while abs(error(e-1))>0.5 && flag==0
    resultado(e,1) = e;
    bandera = 2;
    if error(e-1)==error(e-2)
        if abs(lambda(e-1))==lambda(e-1)
            if resultado(e-1,3)>Pr
                lambda(e) = 1.1*lambda(e-1);
            end
            if resultado(e-1,3)<Pr
                lambda(e) = 0.9*lambda(e-1);
            end
        else
            lambda(e) = -1*lambda(e-1);
        end
    else
        lambda(e) = lambda(e-1)-((error(e-1)*(lambda(e-1)-lambda(e-2)))/(error(e-1)-error(e-2)));
    end
    resultado(e,2) = lambda(e);
    resultado(e,3) = 0;
    for i=1:s
        for j=1:t
            ecuacion(j) = ecuaciones(i,j);
        end
        resultado(e,i+3) = raices(ecuacion,lambda(e),pbmax(i),pbmin(i));
        total = total + resultado(e,i+3);
    end
    resultado(e,3) = total;
    error(e) = total - Pr;
    e = e + 1;
    total = 0;
    if e==100
        flag=1;
        bandera = 5;
    end
end
end
 
 
%Valores de Potencias Mínimas y Máximas de las demás unidades
pmax(1) = 14000;
pmin(1) = 4800;
pmax(2) = 14700;
pmin(2) = 4800;
pmax(3) = 14000;
pmin(4) = 4800;
pmax(4) = 14700;
pmin(4) = 4800;
pmax(5) = 13200;
pmin(5) = 4800;
pmax(6) = 13200;
pmin(6) = 4800;
 
Potmax = Potbmax;
Potmin = Potbmax;
 
for i=1:6
    Potmax = Potmax + pmax(i);
    Potmin = Potmin + pmin(i);
end
 
if Pr>Potmin && Pr<=Potmax
    ecuaciones = [ 0 0 -6.5255493223E-05 1.2341596482E+01;...
                   0 0 -8.0701007582E-05 1.3082714475E+01;...
                   0 0 -3.2271886295E-05 1.1852832530E+01;...
                   0 0 -7.4756331929E-05 1.2900770633E+01;...
   -9.1933325980E-13 4.8142248228E-08 -8.2070689424E-04 1.5230343489E+01;...
   -8.687304E-13 4.543759E-08 -7.767900E-04 1.494327E+01];
 
%1ra iteración
total = 0;
resultado(1,1) = 1;
resultado(1,2) = lambda(1);
resultado(1,3) = 0;
[s t] = size(ecuaciones);
for i=1:s
    for j=1:t
        ecuacion(j) = ecuaciones(i,j);
    end
    resultado(1,i+3) = pbmax(i);
    resultado(1,i+9) = raices(ecuacion,lambda(1),pmax(i),pmin(i));
    total = total + resultado(1,i+9);
end
total = total + Potbmax;
resultado(1,3) = total;
error(1) = total - Pr;
 
%2da iteración
if total>Pr
    lambda(2) = 1.1*lambda(1);
else
    lambda(2) = 0.9*lambda(1);
end
total = 0;
resultado(2,1) = 2;
resultado(2,2) = lambda(2);
resultado(2,3) = 0;
for i=1:s
    for j=1:t
        ecuacion(j) = ecuaciones(i,j);
    end
    resultado(2,i+3) = pbmax(i);
    resultado(2,i+9) = raices(ecuacion,lambda(2),pmax(i),pmin(i));
    total = total + resultado(2,i+9);
end
total = total + Potbmax;
resultado(2,3) = total;
error(2) = total - Pr;
 
%siguientes iteraciones
e = 3;
total = 0;
while abs(error(e-1))>0.5 && flag==0
    bandera = 3;
    resultado(e,1)=error(e-2);
    if error(e-1)==error(e-2)
        if abs(lambda(e-1))==lambda(e-1)
            if resultado(e-1,3)>Pr
                lambda(e) = 1.1*lambda(e-1);
            end
            if resultado(e-1,3)<Pr
                lambda(e) = 0.9*lambda(e-1);
            end
        else
            lambda(e) = -1*lambda(e-1);
        end
    else
        lambda(e) = lambda(e-1)-((error(e-1)*(lambda(e-1)-lambda(e-2)))/(error(e-1)-error(e-2)));
    end
    resultado(e,2) = lambda(e);
    resultado(e,3) = 0;
    for i=1:s
        for j=1;t
            ecuacion(j) = ecuaciones(i,j);
        end
        resultado(e,i+3) = pbmax(i);
        resultado(e,i+9) = raices(ecuacion,lambda(e),pmax(i),pmin(i));
        total = total + resultado(e,i+9);
    end
    total = total + Potbmax;
    resultado(e,3) = total;
    error(e) = total - Pr;
    e = e + 1;
    total = 0;
    if e==100
        flag=1;
        bandera = 6;
    end
end
end
 
if Pr>Potmax
    bandera = 4;
end
 
%Presentación de resultados
if bandera==1
    fprinf('\n');
    fprintf('    La potencia Mínima que se puede generar es de %.2f kW\n\n',Potbmin);
end
 
if bandera==2
    [m n] = size(resultado);
    for i=1:m
        for j=1:n
            tabla(j,i)= resultado(i,j);
        end
    end
    fprintf('\n');
    fprintf('                                            Tabla de Resultado\n\n');
    fprintf('Iteración Lambda  Potencia  B4-Booster-1  B4-Booster-2  B4-Booster-3  B5-Booster-1  B5-Booster-2  B5-Booster-3\n');
    fprintf('                  Requerida      (kW)          (kW)          (kW)          (kW)          (kW)          (kW)n');
    fprintf('                    (kW)\n\n');
    fprintf('%5.0f %10.4f %10.2f %11.2f %12.2f %12.2f %12.2f %12.2f %12.2f\n',tabla);
    fprintf('\n');
    plot(lambda, error,'w');
    hold on
    grid on;
    xlabel('Lambda');
    ylabel('error');
    comet(lambda(1), error(1));
    for i=1:length(lambda)-1
        a=linspace(lambda(i),lambda(i+1),500);
        b=linspace(error(i),error(i+1),500);
        comet(a,b);
    end
end
 
if bandera==3
    [m n] = size(resultado);
    for i=1:m
        for j=1:n
            tabla(j,i)= resultado(i,j);
        end
    end
    fprintf('\n');
    fprintf('                                            Tabla de Resultado\n\n');
    fprintf('Iteración Lambda  Potencia  B4-Booster-1  B4-Booster-2  B4-Booster-3  B5-Booster-1  B5-Booster-2  B5-Booster-3 B1-Booster-1\n');
    fprintf('                  Requerida      (kW)          (kW)          (kW)          (kW)          (kW)          (kW)         (kW)n');
    fprintf('                    (kW)\n\n');
    fprintf('%5.0f %10.4f %10.2f %11.2f %12.2f %12.2f %12.2f %12.2f %12.2f %12.2f %12.2f %12.2f %12.2f %12.2f %12.2f\n',tabla);
    fprintf('\n');
    plot(lambda, error,'w');
    hold on
    grid on;
    xlabel('Lambda');
    ylabel('error');
    comet(lambda(1), error(1));
    for i=1:length(lambda)-1
        a=linspace(lambda(i),lambda(i+1),500);
        b=linspace(error(i),error(i+1),500);
        comet(a,b);
    end
end
 
if bandera==4
    fprintf('\n');
    fprintf('    La Potencia Máxima que se puede generar es de %.2f kW\n\n',Potmax);
end
 
if bandera==5
    [m n] = size(resultado);
    for i=1:m
        for j=1:n
            tabla(j,i)= resultado(i,j);
        end
    end
    fprintf('\n');
    fprintf('                                            Tabla de Resultado\n\n');
    fprintf('Iteración Lambda  Potencia  B4-Booster-1  B4-Booster-2  B4-Booster-3  B5-Booster-1  B5-Booster-2  B5-Booster-3\n');
    fprintf('                  Requerida      (kW)          (kW)          (kW)          (kW)          (kW)          (kW)n');
    fprintf('                    (kW)\n\n');
    fprintf('%5.0f %10.4f %10.2f %11.2f %12.2f %12.2f %12.2f %12.2f %12.2f\n',tabla);
    fprintf('\n\n');
    fprintf('                   Los datos no convergen... intente con otros valores\n\n');
end
 
if bandera==6
    [m n] = size(resultado);
    for i=1:m
        for j=1:n
            tabla(j,i)= resultado(i,j);
        end
    end
    fprintf('\n');
    fprintf('                                            Tabla de Resultado\n\n');
    fprintf('Iteración Lambda  Potencia  B4-Booster-1  B4-Booster-2  B4-Booster-3  B5-Booster-1  B5-Booster-2  B5-Booster-3 B1-Booster-1\n');
    fprintf('                  Requerida      (kW)          (kW)          (kW)          (kW)          (kW)          (kW)         (kW)n');
    fprintf('                    (kW)\n\n');
    fprintf('%5.0f %10.4f %10.2f %11.2f %12.2f %12.2f %12.2f %12.2f %12.2f %12.2f %12.2f %12.2f %12.2f %12.2f %12.2f\n',tabla);
    fprintf('\n\n');
    fprintf('             Los datos no convergen... Itente con otros valores\n\n');
end

Captura3

Captura2
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