% for i = 1:10 % z(i) = x(i) + y(i); % end %%
But in Matlab, we can accomplish the same operation in a single line of % code:
%% z = x + y %%
The syntax isn't always quite as easy as you see above, but in many % cases, vectorization can save you many lines of coding.
% clear all format short %% % %To suppress the output of any expression, simply terminate the expression % with a semicolon ';'. In the following, you'll % only see selected output printed to the command line.
% u = [1,2,3,4,5]; v = [5,6,7,8,9]; w = u + v %% % % Using the semicolon, we can control exactly when we see the output and % when we do not need to see it. % %% Arithmetic operations involving arrays % % We can include arrays in arithmetic operations almost as easily as we can % compute using scalar variables. For most of the examples in this lab, % we will set up a vector containing equally spaced points using the % linspace command. % x = linspace(0,1,11) %% % % To multiply every entry by 2, we can simply write % y = 2*x %% % % This simple command produced a variable y whose entries % are twice that of all the corresponding entries in x. % Here are a few more examples. % z = cos(pi*x) %% w = log(exp(3*x + 1)) %% u = x + y - 4*z %% % % At this point, we can even plot a simple curve. % x = linspace(0,2*pi,200); y = sin(x); plot(x,y) %% % % In the lab on plotting % you can learn how to add a title, axis labels, adjust the axis limits and % the aspect ratio of the plot. % %% The 'dot' operator : Element-wise operations % % You may have noticed that in the above examples, we did not include any % expressions involving the multiplication or division of arrays with each % other. The reason for this is that whereas addition and subtraction and % elementary function evaluation are all well defined mathematical meanings % when applied to arrays, the operations like x*x % are ambiguous. Do we mean a scalar product? Or a matrix multiply in the % linear algebra sense? Or something else? %Often, the correct answer is "something
% else". Suppose we wanted to construct a vector y
% who entries contained the square of each entry of
% x. If we try
%
%
% >> y = x*x %%
we get the error
%% Error using *
% Inner matrix dimensions must agree.
%
% % In fact, we can also get errors using the / % or the ^ operators, as the following example % illustrate. %
% >> 1/x %%
% Error using /
% Matrix dimensions must agree.
%
% % >> y = x^2 %%
% Error using ^
% Inputs must be a scalar and a square matrix.
% To compute elementwise POWER, use POWER (.^) instead.
%
%
%%
%
% The problem is that Matlab is expecting that dimensions of our matrices
% agree in some linear algebra sense. But what we want is to apply our
% operation to each element of the array. To use Matlab terminology, we
% want an element-wise operation. We do this in Matlab by putting
% a "dot" in front of our multiplication, division or exponentiation
% operators. The resulting "dot" operators are '.*', ./ or '.^'. For example, either one of the following
% expressions will give us our desired vector y.
%
x = 0:4
%%
y = x.*x
%%
y = x.^2
%%
%
% We can now take the element-wise inverse of each entry of x:
%
y = 1./x
%%
%
% Using exponentiation with dot operator will also work in this case
%
y = x.^(-1)
%%
%
% You notice that the first entry is the special value Inf, which results when we divide by 0.
% We can now extend our use of the dot operator to more complicated
% expressions. In each of the following examples, we wish to evaluate the
% given expression at an array of values x where x is defined as
%
x = linspace(0,1,11)
%%
%
% Example 1
%
% $$y = \cos(\pi x)\sin(\pi x)$$
% where the variable $x$ is an array.
%
%
y = cos(pi*x).*sin(pi*x)
%%
%
% Example 2
%
% $$y = \frac{\sin(\pi x)}{\cos(\pi x)+2}$$
%
%
%
y = sin(pi*x)./(cos(pi*x)+2)
%%
%
% Example 3
%
% $$y = 2^{10 x}$$
%
%
%
y = 2.^(10*x)
%%
%
% Example 4
%
% $$y = \exp(-10(x-1)^2)^{-1}$$
%
%
%
y = exp(-10*(x-1).^2).^(-1)
%%
%
% or
%
y = 1./exp(-10*(x-1).^2)
%% Lab exercises
%
%
% x = linspace(-1,1,21); %%
% >> [~,E] = ellipke(0.5); % >> fprintf('Length of the ant path : %11.8f\n',2*sqrt(2)*E); %%
% Length of the ant path : 3.82019779 %
For this problem, use the functions diff and sum.
%Here is a sample table showing the distance over the hill, and the % error in the approximation.
%% N Hill Error % ------------------------------------------- % 10 3.8152827260883835 4.9151e-03 % 20 3.8189667565027188 1.2310e-03 % 40 3.8198898891472157 3.0790e-04 % 80 3.8201208051967388 7.6984e-05 % 160 3.8201785425161430 1.9247e-05 % 320 3.8201929773652057 4.8117e-06 % 640 3.8201965861099207 1.2029e-06 %%
Compare your answers with the solutions.
%