%   Your first MATLAB programm starts from here:Learn how to use FOR LOOP.
%   Objective:
%       Given x(1)=1,x(2)=2,...,x(100)=100 and y(1)=1,y(2)=2,...,y(100)=100, compute
%
%       total=x(1)*y(1) + x(2)*y(2) + ... + x(100)*y(100)
%
%   Remarks:
%       (1)This program provides different techniques to do the summation of two number serieses.
%       (2)The later method, the better approache.
%       (3)You can use tic and toc commands to compute the elasped time for each mathod.
%       (4)These methods differ in computing time. Try to user large N to find out the differences.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% 方法一：
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Step 1:create initial values for x and y
clear % clear all previous variables.
x=1:100; %100個x值
y=1:100; %100個y值
total1=0; %set initial total=0
%Step2: use For loop to compute the summation.
for i=1:100
    total1=total1+x(i)*y(i);%這是計算總數的基本技巧：疊加法
end
%Step 3: display the result
total1%display the final result


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% 方法二： Can you see the advantage?
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%Step 1:create initial values for x and y

N=100; %Use a constant variable to represent a number that is repeatedly used in the whole program.
x=1:N; %100個x值
y=1:N; %100個y值
total2=0; %set initial total=0
%Step2: use For loop to compute the summation.
for i=1:N
    total2=total2+x(i)*y(i);%這是計算總數的基本技巧：疊加法
end
%Step 3: display the result
total2%display the final result

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% 方法三： Can you see the advantage?
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%Step 1:create initial values for x and y

% Ask user to enter a constant variable to represent a number that is repeatedly used in the whole program.
N=input('Enter the value of N = '); 
x=1:N; %N個x值
y=1:N; %N個y值
total3=0; %set initial total=0
%Step2: use For loop to compute the summation.
for i=1:N
    total3=total3+x(i)*y(i);%這是計算總數的基本技巧：疊加法
end
%Step 3: display the result
total3%display the final result

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% 方法四： Can you see the advantage?
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%Step 1:create initial values for x and y

% Ask user to enter a constant variable to represent a number that is repeatedly used in the whole program.
N=input('Enter the value of N = '); 
x=1:N; %N個x值
y=1:N; %N個y值
total4=sum(x.*y)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% 方法五： Can you see the advantage?
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%Step 1:create initial values for x and y

% Ask user to enter a constant variable to represent a number that is repeatedly used in the whole program.
N=input('Enter the value of N = '); 
x=1:N; %N個x值
y=1:N; %N個y值
total5=x*y'

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% You may want to try "fprintf" command to print out strings and variable value
disp('********************************************************************');
disp('      total1      total2      total3      total4      total5');
disp([total1 total2 total3 total4 total5])