Matlab provides a friendly interactive environment for scientific programming and visualization. While Matlab is not intended as a substitute for programming languages like Fortran and C, it is very helpful for developing and testing of models, as well as, obtaining immediate feedback on directions for solving difficult problems.
In this section we will introduce many useful constructs used over and over again in Matlab programming.
n=input(' input an integer n = ');
Then issue the command
v=(1:2:n).^2
for {var} = {a vector of counter values} {statements} end
for example
for i=1:3 x(i)=i^2 end
produces x=[1,4,9]. Here is another example
for i=0:4 for j=1:5 a(i+1,j)=1/(i+j); % note use of semicolon to suppress printing %and that an index must be positive. end a end
To do these same operations more efficiently in matlab we can use the following: aa=hilb (5).
if {relation} {statements} end
More generally, you can write
if {relation} {statements} elseif {relation} {statements} else {statements} end
clear % clear the workspace by deleting all variables n=input(' input an integer n = ');
Now we create an n by n matrix A
for i=1:n for j=1:n if i< j A(i,j)=-1; elseif i> j A(i,j)=0; else A(i,j)=1; end end end A
As an illustration of Matlab programmin this matrix can also be built as follows:
AA=eye(n)-triu(ones(n),1) %see help on tril and triu
while {relation} {statements} end
Here is an example
j=1 while j <= 10 k(j)=cos(j*pi); j=j+1; end % to view the matrix type the name of the matrix k % compare this with kk=cos((1:10)*pi)
{matrix expression} {relational operation} {matrix expression}
== equals ~= not equal < less than <= less than or equal > greater than >= greater than or equal
A=[1 2;3 4]; B=[1 2;2 4] T=A==B
returns the matrix
T=[1 1;0 1]
& % and, | % or ~ % not
These are used on matrices obtained from a relational matrix (as above with all zeros and ones). For example,
T1=[1 1;0 1]; T2=[1 0;0 0] T=T1&T2
gives the matrix
T1=[1 0; 0 0]
and
T=T1|T2
gives the matrix
T=[1 1;0 1]
Finally,
T=~T1
gives the matrix
T=[0 0;1 0]
help any
ANY True if any element of a vector is true. For vectors, ANY(V) returns 1 if any of the elements of the vector are non-zero. Otherwise it returns 0. For matrices, ANY(X) operates on the columns of X, returning a row vector of 1's and 0's.
Here is a program the does the same as the any command (see below), i.e., w=any(v) where v returns 1 if any of the elements of the vector v are non-zero. Otherwise it returns 0.
v(5:2:10)=5:2:10; w=0; k=1; while (k<=length(v)&w==0) if v(k)~=0 w=1 end k=k+1; end
This should be compared with
any(v)
As an example of how you might use the any command,
let
M=floor(11*rand(3,4))-1 any(M>=8)
Now consider the all command
help all
ALL True if all elements of a vector are true. For vectors, ALL(V) returns 1 if all of the elements of the vector are non-zero. Otherwise it returns 0. For matrices, ALL(X) operates on the columns of X, returning a row vector of 1's and 0's.
Here is an example
M=floor(11*rand(3,4))-1 all(M<=8)
x=floor(10*rand(1,20)) I=find(x==3) J=find(x<5)
% this is a test m-file called test1.m h=input('mesh size h = '); % inputs a mesh size x=(0:h:1); % computes a grid of x values lx=length(x); % determines the number of elements in the grid y=x.^2; % evaluates the function at the grid points int=(h/2)*( y(1) + 2*sum(y(2:(lx-1))) + y(lx) ) % trap rule for integral with exact value 1/3 Here is a more complicated example of numerical differentiation and plotting
% this is a test m-file called test2.m h=input('mesh size h = '); % inputs a mesh size x=(0:h:2*pi); % computes a grid of x values lx=length(x); % determines the number of elements in the grid y=sqrt(x).*sin(x); % evaluates the function at the grid points dy=diff(y)./diff(x); % numerical derivative of the function plot(x(2:lx),y(2:lx),x(2:lx),dy) text(x(lx-1),dy(lx-1)-.5,' f''(x)'); text(x(lx),y(lx)-.5,'f(x)') grid pause close
diff(z)=z(2:n)-z(1:(n-1))
So
diff(y)./diff(x)
has as its entries (y(x(i+1))-y(x(i)))/(x(i+1)-x(i)) which approximates the derivative of y at x(i+1).
% this is a function file that allows you to choose between two functions function y=fn(x) global prob if prob==1 y=(1-(1-2*x).^2); elseif prob==2 y=(1-2*x).^2; end
Save the file (as fn.m) and now build an m-file to carry out the approximation. The following should be typed into an m-file and saved as fourier.m .
clear global prob prob=input(' pick the problem number for fn.m (1 or 2) = ') N=input('number of terms in Fourier series, N = ') h=.005; t=0:h:1; for n=1:N a(n)= 2*h*fn(t)*sin(n*pi*t)'; % trap rule for the integrals end delta=.05; x=0:delta:1; p=sin((1:N)'*x*pi); yapp=a*p; y=fn(x); err=norm(y-yapp)*sqrt(delta) % gives the L2 norm of difference plot(x,y,x,yapp)
Note that the sine series does not approximate very well a function that is not zero at the ends. Try N=10, 50, 100, 200.
n=input('n= '); w=input('w = '); x=input(' an n-vector, x = ')
Step two: write a for loop program to build a matrix A with the given entries. Then, redo the problem the easy way using matlab syntax:
p_{i,1}=p_{1,j}=1, for i+j <= (n+2), p_{i,j}=p_{i,(j-1)}+p_{(i-1),j}, for i+j > (n+1), p_{i,j}=0
You can use loops but it is much easier to use the built-in commands diag, pascal and rot90.
% pascal(k) builds the pascal matrix of size k by k % rot90(A) rotates the matrix A by 90 degrees % diag(A) builds a column vector from the diagonal of A