This is a script to find appartments in an optimal location with to desired nearby locations.
This is the structure of the parameter file
School: 34.138840, -118.125094, a*r.^2+b*r, {a,b}, [2,1] Climbing: 34.057231, -118.240315, a*r.^2+b*r, {a,b}, [1,1] Hiking: 34.205153, -118.128317, a*r.^2+b*r, {a,b}, [1,1] Downtown_LA: 34.052249, -118.242911, a/r, {a}, [0.01]
Parameters
filename = 'locations.txt';
radius = 0.01;
syms a b c r
Get the information from the parameter file
data = parseLocations(filename);
Generate the cost function
func = @(x,y) 0*x+0*y;
for i = 1:size(data,1)
func = @(x,y)func(x,y) + data{i,4}(sqrt((x-data{i,2}).^2+(y-data{i,3}).^2));
end
Plot the cost function
[x,y] = meshgrid(33.5:0.01:34.5,-118.5:0.01:-118);
z = func(x,y);
surf(z)
Find the optimal location
fmin_f = @(x) func(x(1),x(2));
optimal = fminsearch(fmin_f,[0,0]);
loc = char(vpa(optimal,10));
Open up google maps and zillow
This opens the webpages to show the optimal location with respect to the nearby locations, and apartments in the area
maps = ['https://www.google.com/maps/place/' loc(10:end-3)];
web(maps,'-browser')
small = sprintf('%.6f,%.6f', (vpa(optimal+radius,6)));
big = sprintf('%.6f,%.6f', (vpa(optimal-radius,6)));
loc = [small ',' big];
zillow = ['https://www.zillow.com/homes/for_rent/Pasadena-CA/apartment_duplex_type/' loc '_rect'];
web(zillow,'-browser')
function ret = parseLocations(fn)
syms a b c r
fh = fopen(fn);
line = fgetl(fh);
ret = [];
while line ~=-1
if ~strcmp(line(1),'#')
elem = strsplit(line);
elem{1} = elem{1}(1:end-1);
elem{2} = str2num(elem{2}(1:end-1));
elem{3} = str2num(elem{3}(1:end-1));
elem{4} = matlabFunction(subs(matlabFunction(str2sym(elem{4})),eval(elem{5}), eval(elem{6})));
ret = [ret; elem];
end
line = fgetl(fh);
end
fclose(fh);
end
Published with MATLABĀ® R2018a