How to improve speed of execution?
3 views (last 30 days)
Show older comments
Hello. I'm a student whose been working on a program to find the maxs or mins of a given function with in a specified range using an iterative function that cannot use the min or max function in as few iterations as possible. To this end I have used Halley's method for finding zeros of the derivative of the function, and from there determine the minimum. The function works to my satisfaction, but if an equation takes more than 4 cycles of the loop to complete, the function takes substantially longer. Any Help would be greatly appreciated. The code is:
function [X_final,Y_final]=Lab5_Real_1_4(equation)
x=0;
syms x;
Derivative=diff(equation);
Derivative_2=diff(diff(equation));
Derivative_3=diff(Derivative_2);
Guess1=10-(2*(subs(Derivative,x,10))*(subs(Derivative_2,x,10)))/(2*((subs(Derivative_2,x,10))^2)-((subs(Derivative,x,10))*(subs(Derivative_3,x,10))));
Guess2=4-(2*(subs(Derivative,x,4))*(subs(Derivative_2,x,4)))/(2*((subs(Derivative_2,x,4))^2)-((subs(Derivative,x,4))*(subs(Derivative_3,x,4))));
Guess3=-10-(2*(subs(Derivative,x,-10))*(subs(Derivative_2,x,-10)))/(2*((subs(Derivative_2,x,-10))^2)-((subs(Derivative,x,-10))*(subs(Derivative_3,x,-10))));
iterations=0;
Iterations_required=1;
Guess1_old=10;
Guess2_old=4;
Guess3_old=-10;
while ((iterations<Iterations_required && iterations<=5));
Guess1=Guess1-(2*(subs(Derivative,x,Guess1))*(subs(Derivative_2,x,Guess1)))/(2*((subs(Derivative_2,x,Guess1))^2)-((subs(Derivative,x,Guess1))*(subs(Derivative_3,x,Guess1))));
Guess2=Guess2-(2*(subs(Derivative,x,Guess2))*(subs(Derivative_2,x,Guess2)))/(2*((subs(Derivative_2,x,Guess2))^2)-((subs(Derivative,x,Guess2))*(subs(Derivative_3,x,Guess2))));
Guess3=Guess3-(2*(subs(Derivative,x,Guess3))*(subs(Derivative_2,x,Guess3)))/(2*((subs(Derivative_2,x,Guess3))^2)-((subs(Derivative,x,Guess3))*(subs(Derivative_3,x,Guess3))));
if (abs(Guess1_old-Guess1)>.001 || abs(Guess2_old-Guess2)>.001 ||abs(Guess3_old-Guess3)>.001)
Iterations_required=Iterations_required+1;
end
Guess1_old=Guess1;
Guess2_old=Guess2;
Guess3_old=Guess3;
iterations=iterations+1;
end cycles=iterations
X_array=[Guess1,Guess2,Guess3];
Trial1=subs(equation,x,Guess1);
Trial2=subs(equation,x,Guess2);
Trial3=subs(equation,x,Guess3);
Y_Array=[Trial1, Trial2, Trial3];
Y_Length=length(Y_Array);
Y_sorted=sort(Y_Array);
Y_min=Y_sorted(end-(Y_Length-1));
Y_final=vpa(Y_min)
X_index=find(Y_Array==Y_min);
X_final=vpa(X_array(X_index));
X_final=X_final(1)
1 Comment
Jan
on 30 Sep 2013
Please format your code properly when you want others to read it. See the "? Help" link on this page.
Answers (1)
Jan
on 30 Sep 2013
The main strategy to increase the speed is to avoid repeated calculations. In your case, e.g.
subs(Derivative_2,x,Guess3)
Is evaluated twice. So better use a temporary variable:
c1 = subs(Derivative_2,x,Guess3);
The same happens for other subs() calls also and because this function is the bottleneck of your program, obtaining a speedup of a factor or almost 2 seems possible.
0 Comments
See Also
Categories
Find more on Number Theory in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!