%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Solutions to the Session 5: Introduction to Matlab % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% First things %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Initialize and some comments in advance % To start, we clear the memory, so that the Matlab workspace and the command window are empty clear all clc % Give MATLAB the path, where you want to work. % Otherwise, MATLAB doesn't know where to look for, and the only thing it knows are the 'built-in' functions ... path(path,'C:\Users\pw05v549\Documents\doing15\session5'); % a semicolon after the command means that in your command window the output will be supressed. % a double %% means that you make a section in your code, double %% with some text behind gives you a title of the section to evaluate % to evaluate a section you can press CTRL+ENTER, to evaluate a highlighted part, press F9, otherwise you run the entire code. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% 1 Matrices %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %a) % Either you can use A = [1 3 2;5 0 2]; % or you specify each entry: A(1,1)= 1; ... % or what I use below: A_1 = [1 3 2 ; 5 0 2]; A_2= [1 3 2 5 0 2]; %b) % Matrix of zeros V=zeros(4,2); % Matrix of ones W=ones(3,1); % Identity matrix I= eye(3,3); % Create a vector Z=[1,2,...100]' Z=[1:1:100]'; % Saving the output of a MATLAB run: % % We want to save the output of our exercise in the directory C:\Users\pw05v549\Documents\doing15\session5, % in a fle called Session5.m % % We start by creating the following object: DFILE=['C:\Users\pw05v549\Documents\doing15\session5']; % ==> Output file %and we save it to the directory: save(DFILE); clear; % You retrieve the results via the command load: load 'C:\Users\pw05v549\Documents\doing15\session5'; %c) % An easy way to do this is: % Column vector with 10 normally distributed random variables % RAND([M,N]) returns an M-by-N matrix. R0 = randn(10,1); % Generate values from the uniform distribution on the interval [a, b]. % r = a + (b-a).*rand(100,1); R10 = 10+randn(1,10); % Uniform distribution U = rand(10,1); % A nicer way is to use (works under Matlab R2014a): % R_0 = random('Normal',0,1,10,1); % R_10= 10+random('Normal',0,1,1,10); % U_1 = random('Uniform',0,1,10,1); % You can find the probability distributions on % http://www.mathworks.ch/ch/help/stats/random.html % d) A = A_1; A(1,2) = 8; % e) B = A(2,:); % f) C = [4 1 2; 1 3 3]; D = C(C<=3); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% 2 Basic Manipulations %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% clear %a) E=[4 1;1 3] F=[1 1;2 3] [n,m]=size(E); E+F E-F %b) % Matrix multiplication (not element by element): G=E*F % Multiplication element by element: H=E.*F % Division element by element: I=E./F %c) K=inv(E)*F K=E^-1*F K=E\F % Determinant of a matrix: det(K) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% 3 Basic plotting and statistics %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Matla stores numbers into ROWS N and COLUMS M % x(n rows, m columns) n,m>0 clear %a) % x = random('Uniform',0,100,1,100); % r = a + (b-a).*rand(100,1); % with a being the lower bound and b the upper bound of the distribution x = 0+ (100-0).*rand(1,100); %b) plot(x); %c) x1 = x(1,1:50); x2 = x(1,51:100); plot([x1 x2]) figure(1); subplot(211); plot(x1); subplot(212); plot(x2); %other possibility figure(2); subplot(121); plot(x1); subplot(122); plot(x2); %editing figure(3); plot(x,'r:'); title('100 RANDOM DRAWS'); xlabel('number of the draw'); ylabel('drawn number'); legend('random draws'); axis([0 100 0 100]); %3d mean_x =mean(x); std_x =std(x); mean_x1=mean(x1); std_x1 =std(x1); mean_x2=mean(x2); std_x2 =std(x2); %repeating b) mean_x_vector=zeros(100,1); mean_x_vector(1:100,1)=mean_x; figure(4); hold on ; plot(x,'b'); plot(mean_x_vector,'r--'); title('100 RANDOM DRAWS'); xlabel('number of the draw'); ylabel('drawn number'); legend('random draws','Location','SouthOutside') ; axis([0 100 0 100]); hold off; % You can find everything for editing plots on % http://www.mathworks.ch/ch/help/matlab/creating_plots/using-high-level-plotting-functions.html %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% 4 Working with the Data %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %a) load session5_testdata1.txt -ascii; %check x=session5_testdata1; %b) y=xlsread ('session5_testdata2.xls') ; %c) who clear session5_testdata1 who %d) figure (3); hist(x); figure (4); hist(y); mean_x=mean(x); mean_y=mean(y); std_x=std(x); std_y=std(y) ; %e) corr_xy=corrcoef(x,y) ; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% 6 Preparation for session 9 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %a) g=[0:0.1:10]' size(g) %if you are unsure about the size of g for j=1:101; g(j)=g(j)+0.05; end g %b) A=2 while A<10; A=A+1 end