Simple Commands in MATLAB
Functions
x=0:0.001:1; Enter the range of x values. y=4.*x.*(1-x); Enter the function. Note the .* for multiplying elements of the matrix.
2D Plots
plot(x,y)
To plot two graphs, we define the function(s). For composite functions it would be better to define a function in a file called map1d.m, with the following lines:
function
y=map1d(x)y=4.*x.*(1-x);
Now plot using plot command.
plot(x,map1d(map1d(x)),x,x)
Iteration
To carry out the iteration, one should create a new m-file and save it. One then needs to specify the initial value and loop through the iteration process with a for loop as shown below.
Create a file called iter1d.m with the following lines:
x(1)=0.40; (Starting value – index starts at 1)
for
n=2:100 (loop)x(n)=map1d(x(n-1)); (apply function)
end
(end loop)First clear all of the variables used so far:
clearIn Matlab, type
iter1d
Displaying Results
map1d(.2) ans =0.6400
x(1:10) ans =Columns 1 through 7
0.4000 0.9600 0.1536 0.5200 0.9984 0.0064 0.0255
Columns 8 through 10
0.0993 0.3577 0.9190
plot(x)
Finding Roots –
Fixed point equation: Create a function file map1dP2 to solve g(x)=0 for g(x)=f(f(x))-x and f(x)=2.1*x*(1-x).
function
y=map1dP2(x)z=2.1.*x.*(1-x);
y=2.1.*z.*(1-z)-x;
fzero('map1dP2',.7) Zero found in the interval: [0.476, 0.85839].ans =
0.5238