Borrar filtros
Borrar filtros

Cutting time using optimization instead of a potentially endless for loop ?

2 visualizaciones (últimos 30 días)
Okay, so I have written a code that generates two linear piecewise functions: a hot stream and a cold stream. The hot stream begins below the cold stream and the x axis (temperatures) is fixed. Essentially I am trying to move the hot stream up (positive y direction) until the minimum temperature difference reaches some variable. As of now, I have a for loop that works great, but takes roughly four minutes to run;according to viewer this is all due to the for loop and using ppval inside the for loop. The code I have now for this part is:
dhdc = [];
raise_hot_y = 0;
for i = 1:length(unique_cold_temps)
dhdc(i) = (interpFunction((Hcoldpt(i)+raise_hot_y),hotStreamInterp))- unique_cold_temps(i);
end
dcdh = [];
for i=1:length(unique_hot_temps)
dcdh(i) = interpFunction((Hhotpt(i)+raise_hot_y), coldStreamInterp) - unique_hot_temps(i);
end
while min(min(abs(dhdc)), min(abs(dcdh))) >= epsilon
for i = 1:length(unique_cold_temps)
dhdc(i) = (interpFunction((Hcoldpt(i)+raise_hot_y),hotStreamInterp))- unique_cold_temps(i);
end
for i=1:length(unique_hot_temps)
dcdh(i) = interpFunction((Hhotpt(i)+raise_hot_y), coldStreamInterp) - unique_hot_temps(i);
end
raise_hot_y = raise_hot_y + .1;
end
Is there some way I can use an optimization technique or solver to solve for the raise_hot_y value (how much I must increase the intercept of the function) that is much more time efficient?

Respuestas (1)

Andrew Newell
Andrew Newell el 7 de En. de 2012
Certainly there is, but I'm not sure quite how to formulate it with the information given. It will look something like this. Put your hot and cold points into a single variable, e.g., hotcoldpt, and do the same for your interpolation hotcoldStreamInterp and your target values unique_temps. Vectorize interpFunction and then create an anonymous function:
f = @(x) interpFunction(Hholdcoltpt+x,holdcoldStreamInterp)-unique_temps;
Finally, solve:
raise_hot_sol = fsolve(f,raise_hot_sol_guess);
This finds the solution to f=0.
  2 comentarios
David Hagan
David Hagan el 8 de En. de 2012
I'm not quite sure that would work exactly because the two piecewise functions are completely independant -- I'm not sure what side effects there may be by combining them. However, I did just write up a much better description of my problem (including an image!) that may help greatly if you are interested. I posted it on my website at http://wp.me/p1CeAi-3v . Any help would be greatly appreciated. Thanks!
Andrew Newell
Andrew Newell el 8 de En. de 2012
I'm assuming they are independent. The function |interpfunction| is just a black box that takes some vector |x| and outputs a vector |y|. It could call a different function for each component of the input - if you wished.

Iniciar sesión para comentar.

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by