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

2 views (last 30 days)
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?

Answers (1)

Andrew Newell
Andrew Newell on 7 Jan 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 Comments
David Hagan
David Hagan on 8 Jan 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 on 8 Jan 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.

Sign in to comment.

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!