Does anyone know why this script isn't working?

function Monte_Carlo(N)
% This function implements a simple Monte Carlo solution for
% the two variable Lifeguard Problem.
% N is the number of randomm points
clc % Clear screen
% Define objective function using an anonymous function
T = @(x1,x2) 1/7*sqrt(50^2+x1.^2)+1/4*sqrt(20^2+(x2-x1).^2)+...
1/2*sqrt(30^2+(100-x2).^2);
% Make contour plot
x1=0:100; x2=x1;
[X1,X2]=meshgrid(x1,x2);
Time=T(X1,X2);
contour(x1,x2,Time);grid;hold on
xlabel('x_1'); ylabel('x_2'); title(['N = ' num2str(N) ])
% Make lists of random numbers with uniform distribution
x1=rand(N,1)*100;
x2=rand(N,1)*100;
plot(x1,x2,'ob')
% Calculate corresponding times
time=T(x1,x2);
% Find minimum time and its vector address
[T_star,i]=min(time);
plot(x1(i),x2(i),'+r') % Put a small + at the minimum of random points
plot(81.326,92.472,'+b', 'MarkerSize',15) % Mark global minimum
hold off
disp(['T* = ' num2str(T_star) ])
disp(['x1* = ' num2str(x1(i)) ])
disp(['x2* = ' num2str(x2(i)) ])
return

3 Commenti

What do you mean with not working? When I run this code I don't any error and I don't see any obviously wrong output.
What did you pass in for N? I used 20 and it worked fine:
Hi Rik
Thanks for your reply. I think the problem was the definition for N.

Accedi per commentare.

Risposte (0)

Categorie

Community Treasure Hunt

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

Start Hunting!

Translated by