80 likes | 211 Views
CS 109 C/C++ Programming for Engineers with MATLAB. Administrative Final quiz on wednesday … Final week of lab: optional, help w/ project Final project! Part 1: submit dataset, due tonight @ 11pm Part 2: submit final project Today ? other examples of using MATLAB….
E N D
CS 109 C/C++ Programming for Engineers with MATLAB • Administrative • Final quiz on wednesday… • Final week of lab: optional, help w/ project • Final project! • Part 1: submit dataset, due tonight @ 11pm • Part 2: submit final project • Today? • other examples of using MATLAB… CS 109 -- 28 April 2014
A few more examples, one visual and one not… ans = x: -15.0 y: 8.0 z: 2.0 Solving systems of linear equations transplants-by-state.txt Plotting data onto a map CS 109 -- 28 April 2014
Solving systems of linear equations: linsolve( ) Coefficients Terms -15.0 8.0 2.0 x y z linsolve(Coefficients, Terms) Note that system is always N by N — N equations with N variables. CS 109 -- 28 April 2014
Exercise: • given a file containing *any*system of equations, write afunction to return solution 1.0 3.0 -2.0 5.0 3.0 5.0 6.0 7.0 2.0 4.0 3.0 8.0 function solution = SolveSystemOfEquations(filename) end Assume any system with N equations and N variables… -15.0 8.0 2.0 CS 109 -- 28 April 2014
Plotting data on a map… transplants-by-state.txt CS 109 -- 28 April 2014
MATLAB has built-in maps, and associated data files • USA maps + states • World maps + countries % Enter this command at prompt to see what's available: >> ls(fullfile(matlabroot, 'toolbox', 'map', 'mapdata')) function MapOfContinentalUSA() figure; ax = usamap('conus'); % continental USA: states = shaperead('usastatehi', 'UseGeoCoords', true); faceColors= makesymbolspec('Polygon', ... {'INDEX', [1 numel(states)], 'FaceColor', polcmap(numel(states))}); geoshow(ax, states, 'DisplayType', 'polygon', 'SymbolSpec', faceColors); framemoff; gridmoff; mlabeloff; plabeloff; . . . end
transplants-by-state.txt • How to plot onto map? • Use lat/lon of state… State,Total,Y2012,Y2011,Y2010 Alabama,10347,271,422,433 Arizona,8954,503,735,618 . . . function MapTransplantsByState() . . % draw map as shown on previous slide: . (1) load 'transplants-by-state.txt' (2) for each state si on the map: if we have data for si find total for that state build a string S to display on map textm(si.LabelLat, si.LabelLon, S); end end end CS 109 -- 28 April 2014
transplants-by-state.txt • Complete solution: State,Total,Y2012,Y2011,Y2010 Alabama,10347,271,422,433 Arizona,8954,503,735,618 . . . function MapTransplantsByState() . . % draw map as shown on previous slide: . transplants = dataset('File', 'transplants-by-state.txt', 'Delimiter', ','); fori = 1:numel(states) % for each state, search and plot data: state = states(i).Name; where = strcmp(transplants.State, state) == 1; if sum(where) > 0 % then we have data for this state: data = transplants(where, 'Total'); % search for state's data: total = data{1, 1}; % extract state's total: % build a label of state name + total, and draw on map: text = {state; int2str(total)}; textm(states(i).LabelLat, states(i).LabelLon, text, ... 'HorizontalAlignment', 'center', 'FontWeight', 'bold', ... 'Rotation', 30); end end end CS 109 -- 28 April 2014