You are on page 1of 8

One reason why people like Matlab is that it contains a lot built-in functions, in

most cases, you only need to write a few lines to solve a complicated problem.
The following are some examples, the related toolboxes are given in the end of
each example(you can also use helpwin to see all the toolboxes). If you are not
sure if Matlab can solve your problem, just search on web. For example, if you
want to solve an ODE, you can search on Google with keywords "ode Matlab",
just try and see what you can find.
What's wrong with the following code?
>>A=[2 3; 3 2]
>>B= B = [2 2 2; 3 3 3]
>>B*A
??? Error using ==> mtimes
Inner matrix dimensions must agree.

%Elementary matrices and matrix manipulation.
>>help elmat

Select all the positive elements in a matrix

>>a = rand(5) - 0.5
>>b = a C(a>0).*a

>>whos
>>a > 0

find the points of intersection of two circles and
.
>>S=solve('x^2+y^2=4','(x-1)^2+(y-1)^2=1')
>> [S.x S.y]

>> double([S.x S.y])
>>help solve
>>help symboic

find the minimum of the function on the interval (0,2)
>>help fminbnd
>>help optim

plot a 2d graph
example:
>>edit explot
% copy the following code with ctrl-c and paste it into your file explot.m

x = 1:50;
y1 = sin(x*2*pi/50);
y2 = cos(x*2*pi/50);
hold on;
p1 = plot(x,y1,'k-+');
p2 = plot(x,y2,'r-');

title('Test of plot function');
xlabel('X Axis');
ylabel('Y Axis');

%edit the lines using handles p1 and p2
%get(p1)
%set(p1, 'LineWidth', 5.0);

%edit the axes using handles gca
%get(gca)
%set(gca, 'FontSize', 12, 'FontName', 'Times');



%run it with
>>explot

how to edit the figure
http://www.mathworks.com/access/helpdesk/help/techdoc/index.html?/access/helpdesk/h
elp/techdoc/creating_plots/f9-
54995.html&http://www.mathworks.com/access/helpdesk/help/techdoc/ref/plot.html

If you do not want to edit the figure each time you draw a figure, you can use the handle
of a figure to get and set the properties.

http://www.princeton.edu/~norgaard/MatlabTutorial.pdf

help graph2d
help graph3d

Using Matlab to solve problems in your work.
Q1: Pursuit: At time , 4 persons( ) stand at 4 corners of a square, then
person pursues ( pursues ) with a constant speed . Draw the pathes
of 4 persons. (Note: "pursue" means is always moving towards )

Solve: Assuming the positions of is . Then we can calculate
the velocities of 4 persons.
Magnitude of the velocities: ,
Since is always moving towards , we can calculate the direction of the
velocities:
Then, the velocities can be writen as

Then, we can write the following ODEs
(the ODEs have 8 equations!!!)

The ODEs can be solved in Matlab with ode45 (numerical method)
>>edit pursuit
%copy and paste the following code
>>pursuit

function y = pursuit()

x=[0,0, 1,0, 1,1, 0,1];
t=[0, 0.1];
[T,Y] = ode45(@pursuit_rhs,t,x);
daspect([1 1 1]);
hold on;
plot(Y(:,1),Y(:,2), 'k-', Y(:,3),Y(:,4), 'b-', Y(:,5),Y(:,6), 'r-', Y(:,7),Y(:,8), 'g-');

function dy = pursuit_rhs(t, x)

v=2.0*ones(4,1);
dy=zeros(8,1);
for i=1:1:4
m=2*i-1; %index of person i
n=mod(m+2,8);
%index of persion i+1. Since the last person pursues the first one,we need mod
dy(m:m+1)=v(i)*(x(n:n+1)-x(m:m+1))/norm(x(n:n+1)-x(m:m+1));
%right hand side of ODEs
end

Q2. Solve the poisson equation on the domain
on
in

see
http://en.wikipedia.org/wiki/Discrete_Poisson%27s_equation
for how to solve the equation numerically.

>>edit poisson
%copy and paste the following code.
>>y = poisson(20);

function sol = poisson(n)
% solve the poisson equation
% use n*n mesh on the domain [0,1]*[0,1]

m = n-1;

dh = 1.0/n;
x = dh:dh:1.0-dh;
y = x;

A = make_poisson(m);

spy(A) % view the matrix with graph
pause;

rhs = dh^2*make_rhs(x,y,m); %calculate the right hand side
u = A\rhs; %solve the equation
sol = reshape(u, m, m); %reshape vector u to a matrix

[X, Y] = meshgrid(x, y);
surf(X, Y, sol); %draw the surface
xlabel('X')
ylabel('Y')
zlabel('Z')

function A = make_poisson(m)

n = m*m;
D = zeros(m); %n*n zeros matrix
T = D;

for i=1:1:m
for j=1:1:m
if(abs(i-j) == 1)
D(i,j) = -1;
T(i,j) = -1;
else
if(i == j)
D(i,j) = 4;
end
end
end
end

I = eye(m);

A = kron(I,D) + kron(T,I);
%help kron

function rhs = make_rhs(x, y, m)

rhs = kron(sin(pi*x'), sin(pi*y')); %help kron

You might also like