Advertisement
makispaiktis

Course 2 - Define variables and objective function

Aug 28th, 2023 (edited)
1,269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 0.73 KB | None | 0 0
  1. clear all
  2. close all
  3. clc
  4.  
  5. % 1. Basics
  6. prob = optimproblem("Description","Factory Location");
  7. x = optimvar("x");
  8. y = optimvar("y");
  9. X = [5 40 70];
  10. Y = [20 50 15];
  11.  
  12.  
  13. % 2. Objective
  14. d = sqrt((x - X) .^ 2 + (y -Y) .^ 2)        % 1 x 3 vector containing d1, d2, d3
  15. dTotal = sum(d)
  16. prob.Objective = dTotal;
  17. show(prob)
  18.  
  19. % 3. Visualize
  20. xvec = linspace(0,75);
  21. yvec = linspace(0,75);
  22. [x,y] = meshgrid(xvec,yvec);
  23. distance = sqrt((x-X(1)).^2 + (y-Y(1)).^2)+...
  24.     sqrt((x-X(2)).^2 + (y-Y(2)).^2)+...
  25.     sqrt((x-X(3)).^2 + (y-Y(3)).^2);
  26. contourf(x,y,distance)
  27. ylabel("Y-Coordinate")
  28. xlabel("X-Coordinate")
  29. hold on
  30. plot(X(1), Y(1), 'redX');
  31. hold on
  32. plot(X(2), Y(2), 'redX');
  33. hold on
  34. plot(X(3), Y(3), 'redX');
  35. hold on
  36. colorbar
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement