%-- 9/03/08 1:18 PM --% % notice that a % symbol comments out the rest of that line which is not executed % simple matrix manipulations x=[1 2 3 4 5] x=[1 2 3 4 5]; x' x*x' x'*x A=x'*x % extracting parts of a matrix A(:,3) A(3,:) % using an implied loop x=1:5 x=1:8 x=1:2:8 x=0:.01:2*pi; plot(x,sin(x)) plot(x,sin(x),'r--^') x=0:0.1:2*pi; plot(x,sin(x),'r--^') plot(x,sin(x),'r--^','markerfacecolor','r') plot(x,sin(x),'r--^','markerfacecolor','r','markersize',8) plot(x,sin(x),'r--^','markerfacecolor','r','markersize',2) plot(x,sin(x),'r--^','markerfacecolor','r','markersize',3) xlabel(' this is the x of \beta ') clear % clears all the work space whos clf % solve y"= 1 y(0)=0=y(1) by finite differences x=0:.1:1 N=length(x) whos o=ones(N,1) o=zeros(N,1) o=zeros(1,N) A=diag(-2*ones(N-2,1),0) A=diag(-2*ones(N-2,1),0)+diag(ones(N-3,1),1) A=diag(-2*ones(N-2,1),0)+diag(ones(N-3,1),1)+diag(ones(N-3,1),-1) b=ones(N-2,1) Y=zeros(N,1) Y(2:N-1)=A\b % note solution is inserted into part of the vector Y % oops wrong answer as we forgot the dx^2 factor dx=0.1 b=b*dx^2 Y(2:N-1)=A\b plot(x,Y,'b-^',x,0.5*x.*(x-1),'r--o') % <--- note the .* here % A slicker piece of code would be (copy and paste the following into the command window and wait and watch): clear;clf; dx = 0.1; x = 0:dx:1 N = length(x) A = diag(-2*ones(N-2,1),0)+diag(ones(N-3,1),1)+diag(ones(N-3,1),-1); b = dx^2*ones(N-2,1) Y = zeros(N,1) Y(2:N-1) = A\b % note solution is inserted into part of the vector Y plot(x,Y,'b-^',x,0.5*x.*(x-1),'r--o') % <--- note the .* here % using Matlab help help eig eig(A) [V,D]=eig(A) whos clear % the .* .? and .^ operations x=1:5 y=2*x x*y x.*y x./y x.^5 log(x)