0% found this document useful (0 votes)
13 views

Matlav Code Assignment

The document describes a MATLAB function that calculates the square root of a positive number using Newton's method of iterative approximation. The function checks if the input is positive, sets the initial guess and termination criteria, and runs Newton's method to compute the square root. The code provided tests the function on sample inputs of 729, 1500, and -72 to demonstrate it works for positive and negative cases.

Uploaded by

kthshlxyz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Matlav Code Assignment

The document describes a MATLAB function that calculates the square root of a positive number using Newton's method of iterative approximation. The function checks if the input is positive, sets the initial guess and termination criteria, and runs Newton's method to compute the square root. The code provided tests the function on sample inputs of 729, 1500, and -72 to demonstrate it works for positive and negative cases.

Uploaded by

kthshlxyz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

AGUILAR, KEITH ANGELINE A.

BSCE 3C

ASSIGNMENT CE 101-C3-2

Determining the square root of a number p, , is the same as finding a solution to the equation
. Write a MATLAB user-defined function that determines the square root of a positive number
by solving the equation using Name the function . The output argument
Xs is the answer, and the input p argument is the number whose square root is determined. The program should
include the following features:

• It should check if the number is positive. If not, the program should stop and display an error message.

• The starting value of x for the iterations should be .

• The iterations should stop when the estimated relative error is smaller than .

• The number of iterations should be limited to 20. If a solution is not obtained in 20 iterations, the program
should stop and display an error message.

Use the function to determine the square root of:

Function Xs=SquareRoot(p)
% check whether the given value is positive
if p<=0
error('Input must be a positive number');
end
% set the starting value of x for the iteration
x=p;
% set the allowed maximum number of iterations
maxIterations=20;
% set the allowed relative error
tolerance=1e-6;
% use newton's method for the iteration
for iteration=1:maxIterations
x_new = 0.5*(x+p/x);
% Check for the proximity using relative error
if abs(x_new-x)/x<tolerance
Xs=x_new;
return;
end
% Update the current value
x=x_new;
end

clear, clc, close all

1
% For (a) 729
% Check if the number is positive
p = input('Enter the number to be squared:');
if (p < 0)
disp('Error! The number is negative.');
else
disp('Hooray! The number is positive.');
end

Hooray! The number is positive.

% Name the function


if (p>0)
Xs = sqrt(p)
end

Xs = 27

% The starting value of x for the iteration


x = p;
% The function
f = @(x) x.^2-p;
% The derivative of the function
df = @(x) 2*x;
% The estimated relative error
e = 10.^-6;
% The limitation of number of iteration
n = 20;
% Running a loop for i = 1:20
if p > 0
for i=1:n
xi = x - f(x)./df(x);
fprintf('x%d = %.6f\n',i,xi)
if (abs((xi-x)/x)<e)
break
end
x = xi;
end
else
disp('Error! Solution is not obtained.');
end

x1 = 365.000000
x2 = 183.498630
x3 = 93.735706
x4 = 50.756446
x5 = 32.559577
x6 = 27.474651
x7 = 27.004100
x8 = 27.000000
x9 = 27.000000

clear, clc, close all

2
% For (b) 1500
% Check if the number is positive
p = input('Enter the number to be squared:');
if (p < 0)
disp('Error! The number is negative.');
else
disp('Hooray! The number is positive.');
end

Hooray! The number is positive.

% Name the function


if (p>0)
Xs = sqrt(p)
end

Xs = 38.7298

% The starting value of x for the iteration


x = p;
% The function
f = @(x) x.^2-p;
% The derivative of the function
df = @(x) 2*x;
% The estimated relative error
e = 10.^-6;
% The limitation of number of iteration
n = 20;
% Running a loop for i = 1:20
if p > 0
for i=1:n
xi = x - f(x)./df(x);
fprintf('x%d = %.6f\n',i,xi)
if (abs((xi-x)/x)<e)
break
end
x = xi;
end
else
disp('Error! Solution is not obtained.');
end

x1 = 750.500000
x2 = 376.249334
x3 = 190.118026
x4 = 99.003931
x5 = 57.077422
x6 = 41.678758
x7 = 38.834157
x8 = 38.729974
x9 = 38.729833
x10 = 38.729833

3
clear, clc, close all
% For (c) -72
% Check if the number is positive
p = input('Enter the number to be squared:');
if (p < 0)
disp('Error! The number is negative.');
else
disp('Hooray! The number is positive.');
end

Error! The number is negative.

% Name the function


if (p>0)
Xs = sqrt(p)
end
% The starting value of x for the iteration
x = p;
% The function
f = @(x) x.^2-p;
% The derivative of the function
df = @(x) 2*x;
% The estimated relative error
e = 10.^-6;
% The limitation of number of iteration
n = 20;
% Running a loop for i = 1:20
if p > 0
for i=1:n
xi = x - f(x)./df(x);
fprintf('x%d = %.6f\n',i,xi)
if (abs((xi-x)/x)<e)
break
end
x = xi;
end
else
disp('Error! Solution is not obtained.');
end

Error! Solution is not obtained.

You might also like