-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLearningMATLAB.m
More file actions
190 lines (156 loc) · 5.55 KB
/
LearningMATLAB.m
File metadata and controls
190 lines (156 loc) · 5.55 KB
1
%MatricesA = [3 2;4 5;27 17]B = [4 7 6; 1 2 3]C = A*BD = A+B %does not run; dimensions of matrices are not equalB' %Transpose of B%{VectorsVectors are just matrices with one of the dimensions equal to oneA row vector has only one rowA column vector has only one columnFor example:%}R = [3 4 19 27 2] %row vectorC = R' %column vectorC = R; %The above command echoed C, but this does not b/c of the semicolon%{Creating a vector with internal MATLAB commandsVector = start:increment:end (ending value is always less than or equal to end)Vector = zeros(row, col); all entries are zeroVector = ones(row, col); all entries are oneVector = rand(row, col); pseudo-random entries from [0,1] (randn from normal)%}t = 1:1:365; %creates a row vector with start 1, increment 1, and end 365s = 1:2:365; %creates a row vector with start 1, increment 2, and end 365r = 1:2:364; %creates a row vector with start 1, increment 2, and end 363q = zeros(3, 4)p = ones(5, 1)n = rand(1, 10)n*10 %rand from [0,10]%MATLAB built in functionstemp = 15*sin(2*pi*t/365)+12; %seasonal temp variable%graphics to visualize resultplot(t, temp, '+');xlabel('Time (days)');ylabel('Smoothed temperature (degrees C)')helphelp plot%help item%plotting vectorsM = [4 83 5 13 12];plot(M)R = sort(R);M = sin(R);plot(R, M)title('M vs R')%Working with data frames - the below doesn't work. Why not? B/c i had blanksload /Users/drewwadsworth/Documents/Education/"Menlo School"/"Menlo 11"/"AP Stats"/Pset4_Sem2_APStats/Pset4RStuffs.csvload /Users/drewwadsworth/Documents/Pset4RStuffs.csv%df(:,2) means "all rows from column 2 of df"ace = Pset4RStuffs(:,1);noAce = Pset4RStuffs(:,2);plot(Pset4RStuffs) %individually plots columnsplot(ace, noAce, 'or') %plots noAce as function of acehist(ace, binwidth=5) %histogram of ace w binwidth 5%solving Systems of equations A = coefficient matrix B = sums vector xy = A\B%operations conducted element by element require a '.' after the vector name e.g. c.*cM = M(2:4) %A(r1:r2, c1:c2) subsetrc = diff(M) %diff has size one less than its parameter%ezplot plots functionsezplot(@(X) sin(X))ezplot(@(X,Y) sin(X) - Y)%Practice Problems%% 1load /Users/drewwadsworth/downloads/ppt.dat %df for these problems%% 2help sum %sums along the first non singleton dimension of a matrixpptSum = sum(ppt'); %summed the transpose for yearsyears = 1960:1:1989;plot(years, pptSum, 'o')title('Annual Precipitation from 1960 to 1989')xlabel('Year')ylabel('Total Annual Precipitation')%% 3pptMean = mean(ppt) %monthly mean precipitationbar(pptMean)month = {'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'};%The below sets the label for each x tick according to the given vectorset(gca, 'XTickLabel', month)title('Mean Monthly Precipitation from 1960 to 1989')xlabel('Month')ylabel('Mean Monthly Precipitation')%% normal plot with error bars showing standard deviationhelp errorbar %creates a normal plot with specified error barshelp std %standard deviation of a vector - can also be applied to matricesstd(ppt)errorbar(pptMean, std(ppt), 'xr')set(gca, 'XTick', 1:12) %sets tick spacing to every one from the entries 1-12set(gca, 'XTickLabel', month)title('Mean Monthly Precipitation from 1960 to 1989 with One Standard Deviation Error Bars')xlabel('Month')ylabel('Mean Monthly Precipitation')%% Comparing monthly precipitation mean and medianhelp median %like mean or std but for medianplot(pptMean-median(ppt), 'x') %plotting difference between mean and median of monthly precipitationset(gca, 'XTick', 1:12)set(gca, 'XTickLabel', month)title('Difference in Mean and Median for Monthly Precipitation')xlabel('Month')ylabel('Mean Minus Median')%% 4help reshape %reshapes a matrix into a matrix of different dimensionslinearPPT = reshape(ppt, 1, 360)bar(linearPPT)set(gca, 'XTick', 1:360) %Necessary for a 1 variable plot to change x ticks this way%% Graph for 30 years w mean as a bar graphfigure(1); clf;%Bar graph of monthly meansbar(pptMean)month = {'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'};%The below sets the label for each x tick according to the given vector%set(gca, 'XTickLabel', month)hold on %keep the above bar graphfocusYear = 1972; %Year highlighted on the graphstartingYear = years(1); %First Year graphed%line graphs of each yearfor i=1:30 if i != focusYear - startingYear + 1 %c = [(i/60) + 0.4, (i/60) + 0.4, (i/60) + 0.4]; p1 = plot((1:12), ppt(i,:), 'Color', [0.5, 0.5 ,0.5]); else c = [1, 0, 0]; pf = plot((1:12), ppt(i,:), 'Color', c, 'LineWidth', 2); endendset(gca, 'XTickLabel', month)%Creating a legendlegend([p1 pf], [num2str(years(1)), ' - ', num2str(years(length(years)))], [num2str(focusYear)])title('Monthly Precipitation from 1960 to 1989')xlabel('Month')ylabel('Monthly Precipitation')%% 5figure(1); clf;fflush(stdout)numYear = input("What year would you like?");1962startingYear = years(1);indexOfYear = numYear - startingYear + 1;year = ppt(indexOfYear, :);%help min['Minimum precipitation: ', num2str(min(year))]['Maximum precipitation: ', num2str(max(year))]['Mean precipitation: ', num2str(mean(year))]%help stemstem(year)title(['Monthly Precipitation for ', num2str(numYear)])xlabel('Month')ylabel('Precipitation')set(gca, 'XTick', 1:12)set(gca, 'XTickLabel', month)%% 6%% Linear Regressionlm = polyfit(years, pptSum, 1)%model: y = 915.04590 + 0.12603xvariabilityPercent = (corr(pptSum, lm(2) + lm(1)*years))^2%Using corr instead of corrcoef because apparrently corrcoef isn't supported