NUMERICAL ANALYSIS LAB
NS-321L
LECTURE – 5: PROGRAMMING - I
Write some lines of code that will
calculate the sum 1+2+3+...+300.
Hint: for loop
% Matlab code to sum the first
300 numbers
s = 0;
for i=1:300,
s = s + i;
end
disp(s)
Modify your program to sum the first N numbers,
and set N equal to 1000, 10000, and 100000 to see
what sum you get.
% Matlab code to sum the first N
numbers
N = 100000;
s = 0;
for i=1:N,
s = s + i;
end
s
Relational Operators in MATLAB
EXAMPLE PROGRAM:
a = input('Enter number: ');
if (a == 1)
disp('one')
else
disp('not one')
end
Logical Operators in MATLAB
EXAMPLE:
a = input('Enter number: ');
b = input('Enter number: ');
if (a&&b == 1)
disp('both non zero')
else
disp('at least one number is zero')
end
IF… Else Exercise
1. Write a program to check whether a number is negative, positive or
zero.
2. Write a program to input month number and print number of days in
that month
Assignment
•Write some lines of code to calculate the sum 12 + 22 +32 + ... 4002.
•Write some lines of code to calculate the sum 1·2+2·3+3·4+ ... + 249·250.
•Write a program to input angles of a triangle and check whether triangle is
valid or not.
•Write a program to find all roots of a quadratic equation.