15/7/22 4:54 PM D:\CBCS\Practical\4th sem matla...\Sum.
m 1 of 1
Program
% Calculate the sum 1/1 + 1/2 + 1/3 + 1/4 + . . . + 1/N.
clc()
i=1; total = 0.0;
n = input('Input the value of N: ');
while i <= n
total = total + 1/i;
i = i + 1;
end
fprintf("1 + 1/2 + 1/3 . . . + 1/%d = %f \n",n,total);
Output:
Input the value of N: 37
1 + 1/2 + 1/3 . . . + 1/37 = 4.201586
>>
15/7/22 7:04 PM D:\CBCS\Practical\4th sem matla...\Abs.m 1 of 1
% Absulute value of any number
x = input('input a number: ');
if(x<0)
x=-x;
end
fprintf("absolute value is %d \n",x)
Outputs:
>> Abs
input a number: -2
absolute value is 2
>> Abs
input a number: 3
absolute value is 3
>> Abs
input a number: 0
absolute value is 0
15/7/22 7:07 PM D:\CBCS\Practical\4th sem...\ascending.m 1 of 1
% ascending order of numbers
clc()
x = input('input numbers in a row matrix ');
n = length(x);
for i = 1 : n-1
for j = i+1 : n
if(x(i)>x(j))
temp = x(i);
x(i)=x(j);
x(j)=temp;
end
end
end
disp('Ascending order: ');
disp(x)
Output:
input numbers in a row matrix [1 5 4 3 6 5 4 3 7 6 5 9 2 5 7]
Ascending order:
1 2 3 3 4 4 5 5 5 5 6 6 7 7 9
15/7/22 7:01 PM D:\CBCS\Practical\4th sem ...\new_raph.m 1 of 1
function root = new_raph(f,xi,tol)
syms x; i=0;
df = eval(['@(x)' char(diff(f(x)))]);
while(abs(f(xi))>tol)
fprintf('x%d = %.10f \n',i,xi);
xi = xi - f(xi)/df(xi);
i=i+1;
end
fprintf('x%d = %.10f \n',i,xi);
end
Output:
>> new_raph(@(x)x^2-3*x+1, 2, 0.00001)
x0 = 2.000000
x1 = 3.000000
x2 = 2.666667
x3 = 2.619048
x4 = 2.6180344478
>>
15/7/22 7:18 PM D:\CBCS\Practical\4th sem ma...\Secant.m 1 of 1
% Secant Method in MATLAB
clc();
a=input('Enter function: ','s');
f=inline(a)
x(1)=input('Enter first point of guess interval: ');
x(2)=input('Enter second point of guess interval: ');
n=input('Enter allowed Error in calculation: ');
iteration=0;
for i=3:1000
x(i) = x(i-1) - (f(x(i-1)))*((x(i-1) - x(i-2))/(f(x(i-1)) - f(x(i-2))));
iteration=iteration+1;
if abs((x(i)-x(i-1))/x(i))*100<n
root=x(i)
iteration=iteration
break
end
end
Output:
Enter function: x^2-4*x+3
f =
Inline function:
f(x) = x^2-4*x+3
Enter first point of guess interval: -10
Enter second point of guess interval: 10
Enter allowed Error in calculation: 0.00001
root =
3.000000000000002
iteration =
12
15/7/22 6:31 PM D:\CBCS\Practical\4th sem matlab - Numerical Methods\Nu...\regulafalsi.m 1 of 1
function root = regulafalsi(f,a,b,tol)
format long;
if (nargin < 4)
error('Wrong number of input arguments');
elseif (argin == 4)
i = 0;
g = 1;
disp('Iteration a b c f(a) f(b) f(c)');
disp('========= ======= ======= ======= ========== ========== ========');
while(abs(g) > tol)
i = i + 1;
c = a - ((f(a)*(b-a))/(f(b) - f(a)));
if(f(c)*f(a) > 0)
b = c;
g = f(b);
root = b;
else
a = c;
g = f(a);
root = a;
end
fprintf('%3d%17.7f%11.7f%11.7f%14.7f%14.7f%14.7f\n', i,a,b,c,f(a),f(b),f(c));
end
elseif (nargin > 4)
error('Too many input arguments');
end
Output:
>> regulafalsi(@(x)x^2-3,-3,4,0.001)
Iteration a b c f(a) f(b) f(c)
========= ======= ======= ======= ========== ========== ========
1 -3.0000000 -9.0000000 -9.0000000 6.0000000 78.0000000 78.0000000
2 -3.0000000 -2.5000000 -2.5000000 6.0000000 3.2500000 3.2500000
3 -3.0000000 -1.9090909 -1.9090909 6.0000000 0.6446281 0.6446281
4 -3.0000000 -1.7777778 -1.7777778 6.0000000 0.1604938 0.1604938
5 -3.0000000 -1.7441860 -1.7441860 6.0000000 0.0421850 0.0421850
6 -3.0000000 -1.7352941 -1.7352941 6.0000000 0.0112457 0.0112457
7 -3.0000000 -1.7329193 -1.7329193 6.0000000 0.0030091 0.0030091
8 -3.0000000 -1.7322835 -1.7322835 6.0000000 0.0008060 0.0008060
ans =
-1.732283464566929