Practical 3: Arrays
Array Multiplication Expressions
Exercise code
%% Practical 3 debugging exercise
% Add your name and student ID here
% Remember you can suppress output by adding the ;
%% Part 1
% Create an array that contains the cubic roots
% of all elements of array A
clear all
A = [0 1 8 27 64]
cubic_root = A^(1/3) % **** There is a dot missing here
%------------------------------------------------
%% Part 2
% Plot & sum an array containing the series 1/n^2
% for integers from 1 to 100
clear all
x=[1:100]
y=1/x^2 % **** There are two dots missing here
plot(x,y)
sum(y) % The answer should be 1.6350
%------------------------------------------------
%% Part 3
% Conjecture that the limit of sin(x)/x = 1 as x -> 0
% by evaluating sin(x)/x for x = 0.1, 0.01,., 0.00000001
% Find the positions where it is 1 for the given precision
clear all
format long % uses 16 digits for each value
x=0.1^[1:8] % **** One dot missing here
y=sin(x)/x % **** One dot missing here
error = 1-x(8)
c=find(y>=error) %finds indices of elements of array that are bigger
%than defined value of error
plot(x,y)
%------------------------------------------------
%% Part 4
% Find out why this code is not working
% Update the value of maxX to make it work properly
clear all
maxX = 9 % change this value
X=[1:2:maxX]
Y=[10:-1:1]
X+Y
%------------------------------------------------
%% Part 5
% Find the sum of the series (n+1)*n^(1/3)
% for n = 1 to 100
% Your answer should be 2.047524258847828e+004
clear all
n_plus_1 = [2:101]
n_cubic_root = [1:100]^(1/3) % **** One dot is missing here
% note that the above line is a shorter version of part 1 sum_of_series = sum(n_plus_1*n_cubic_root) % **** add dot for the %array multiplication