Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Value = Approx_Integral(F, x_Min, x_Max)
- Approximation = [];
- if(x_Min <= x_Max)
- %f_Min = fminbnd(F,x_Min,x_Max) this wasn't returning the right
- %values and I'm not sure why
- x=x_Min:.00001:x_Max;
- f_Min = min(F(x));
- f_Max = max(F(x));
- for k = 1:10 %creates ten seperate approximations and will eventually take the mean of all of them
- Random_Pointsx = rand(1,ceil((x_Max-x_Min)*10000))*(x_Max-x_Min)+x_Min;
- Random_Pointsy = rand(1,ceil((x_Max-x_Min)*10000))*(f_Max-f_Min)+f_Min;
- Random_Points = [Random_Pointsx; Random_Pointsy];%generates the random
- %points within the box containing the section of the function
- hits = 0;
- for i = 1:(x_Max-x_Min)*10000
- if(Random_Points(2,i)< 0)
- if(Random_Points(2,i) >= F(Random_Points(1,i)))
- hits = hits -1;%area below y=0 is subtracted
- end
- else if (Random_Points(2,i) <= F(Random_Points(1,i)))
- hits = hits + 1;%area above y=0 is added
- end
- end
- end
- Approximation = [Approximation, hits/(x_Max*10000)*(x_Max-x_Min)*(f_Max-f_Min)];
- %adds current iteration's approximation to the list of k
- %approximations
- end
- Value = mean(Approximation);%takes the mean of the k approximations
- else
- Value = -1*Approx_Integral(@(x)F(x), x_Max, x_Min);
- %since b<a, switches bounds and multiplies by -1
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement