Computer programming
Problem 8
for i=1:3
for k =1:5
a(i,k)=(i-k)/(i+k);
end
end
a
= = = = == = = =
Chapter7
Computer programming
Problem 10
m = input('Enter the number of terms: ');
s=0;
for n = 1:m
s = s+ (-1)^n/n^2;
end
series=sqrt(12*s)
= = = = = ===
Chapter7
Computer programming
Chapter7
Problem 11
x = [15 -6 0 8 -2 5 4 -10 0.5 3];
n = length(x);
sumpos=0;
sumneg=0;
for i = 1:n
if x(i)<0
sumneg=sumneg+x(i);
else
sumpos = sumpos+x(i);
end
end
fprintf('The sum of negative numbers in vector =
%5.2f \n',sumneg);
fprintf('The sum of positive numbers in vector =
%5.2f \n',sumpos);
= = = == = = = = = = =
Computer programming
Problem 12
function GM = Geomean(x)
n = length(x);
s=1;
for i=1:n
s = s*x(i);
end
GM = s^(1/n);
=========
Chapter7
Computer programming
Problem 13
function y = fact(x)
if x<0 | rem(x,1)~=0
disp('Error: Positive integer number must be
entered');
elseif x == 0
y = 1;
else
y = 1;
for i=1:x
y = y*i;
end
end
========
Chapter7
Computer programming
Chapter7
Problem 14
function y =cosTaylor(x)
xr = x*pi/180;
n = 0;
s = 1; % first term (n=0)=1
E=1; % E must be defined(E>0.000001) to enter while loop
while E>=0.000001
n = n+1;
an = (-1)^n*(xr)^(2*n)/factorial(2*n);
s = s+an;
E = abs(an);
end
y = s;
===========
Computer programming
Chapter7
Problem 15
clc
n=1;
while rem(n,2)~=0 | rem(n,7)~=0 | n^3<40000
n = n+1;
end
fprintf('The required number is %d \n',n)
=============
Computer programming
Problem 17
function y = downsort(x)
n = length(x);
for i = 1:n
for k = i+1:n
if x(k)>x(i)
temp=x(k);
x(k)=x(i);
x(i)=temp;
end
end
end
y = x
== ======
Chapter7
Computer programming
Chapter7
Problem 26
function y = cubic(P)
x0=P; % initial value of solution to be improved.
E = 1; % initial value of error >0.00001 to enter while
loop
while E>=0.0001
x1 = x0 - (x0^3-P)/(3*x0^2);
E = abs((x1-x0)/x0);
x0=x1;
end
y = x1;
% fprintf('The cupic root of %i is % 5.2f ',P,x1);
© Copyright 2026 Paperzz