NSM Practical Booklet (Itscholar - Codegency.co - In)
NSM Practical Booklet (Itscholar - Codegency.co - In)
Practial 2.A
Root using Bisection Method
clc //clear console screen
clear //clear memory data
////Input : Bisection(f,a,b)
/ f = Input Function; a = initial iteration Root ; b = Final iteration Root
/ Output = Root
tol=1.e-4;
//tol=Maximum error between iterations that can be tolerated
maxit=50; // maxit = Maximum number of Iteration
Ea = 1; // Assume Initials error is 1
n = 1; // start with First iteration
/ process end if any one of the condition satisfied else continue for next
iteration
n = n + 1;
end
end
/ last value of c will give output as root once while loop stop
root = c(n);
endfunction
root = bisection(f1,a,b);
root = round( root*10^5)/10^5; //Round the final answer upto
5 decimal point as ^5
disp(root,"root of the equation = ")
Output :
////Input : regulafalsi(f,a,b)
/ f = Input Function; a = initial iteration Root ; b = Final iteration Root
/ Output = Root
/ process end if any one of the condition satisfied else continue for next
iteration
n = n + 1;
end
end
/ last value of c will give output as root once while loop stop
root = c(n);
endfunction
root = regulafalsi(f1,a,b);
root = round(root*10^5)/10^5; //Round the final answer upto 5
decimal point as ^5
disp(root,"root of the equation = ")
Output :
////Input : Newtonraphson(f,df)
/ f = Input Function; df=Derivative of function
/ Output = Root
/ last value of c will give output as root once while loop stop
root = x(n+1);
endfunction
root = newtonraphson(f1,df1);
root = round( root*10^5)/10^5; //Round the final answer upto
5 decimal point as ^5
disp(root,"root of the equation = ")
Output :
Scilab code
Practial 3.A1
Create Forward Difference table
clc // Clear your console screen
clear //Clear your previous Data
del = %nan *ones(n ,5) ; // Find del = %nan = not an integer and
ones = returns a (m1,m2) matrix full of ones.
end
end
del (: ,1) = [ ]; //return total Matrix in del
Output :
del = %nan *ones(n ,6) ;// Find del = %nan = not an integer and
ones = returns a (m1,m2) matrix full of ones upto d5y since we take
6.
del (: ,1) = y'; //First column of del count as dy/dy = y'
Output :
del = %nan *ones(n ,5) ; // Find del = %nan = not an integer and
ones = returns a (m1,m2) matrix full of ones last till 4. Take only
upto value is exist else output is nan..
del (: ,1) = y'; //First column of del count as dy/dx = y'
Output :
x y dy d2y d3y d4y d5y 10. 46. 20. - 5. 2.
- 3.
20. 66. 15. - 3. - 1. Nan
30. 81. 12. - 4. Nan Nan
40. 93. 8. Nan Nan Nan
50. 101. Nan Nan Nan Nan
f ( 1 2 ) = 50.5968
Practial 3.B 2
Newton backward Difference Formula
clc // Clear your console screen
clear //Clear your previous Data
mprintf( "%5s %7s %8s %9s %8s %8s " , ' x ' , ' y ' , ' dy ' , 'd2y ' , '
d3y ' , ' d4y ')
//print value in form of x, y, dy, d2y, d3y d4y and so on
//%5s means 5 space between dy d2y %7s mean 7 space
between d2y d3y & so on
disp([x' del]) //Output x y Del(matrix form)
Output :
x y dy d2y d3y d4y d5y 10. 46. 20. - 5. 2.
- 3.
20. 66. 15. - 3. - 1. Nan
30. 81. 12. - 4. Nan Nan
40. 93. 8. Nan Nan Nan
50. 101. Nan Nan Nan Nan
f ( 4 2 ) = 95.0048
Practial 3.C
Lagranges Formula
clc // Clear Screen
clear //Clear previous data
del = %nan *ones(n ,4) ; // Find del = %nan = not an integer and
ones = returns a (m1,m2) matrix full of ones last till 4. Take only
upto value is exist else output is nan..
del (: ,1) = y'; //First column of del count as dy/dy = y'
Output :
f(2.3)
→
--- 2.4202
Practical 4.A
Guass Jordan Method
clc
clear
/ Enter Coefficient of Matrix A i.e. Coefficient x,y,z,
A=[121;234;432];
/ Enter R.H.S. Value
B = [8; 20; 16];
//Diagonal Normalization
for j =1: n
Aug (j ,:) = Aug (j ,:) / Aug (j ,j);
end
/ Compare A with
B x = Aug (: , n +1) ;
Output :
1. 2. 1. 8.
0. -1. 2. 4.
4. 3. 2. 16.
1. 2. 1. 8.
1. -1. 2. 4.
0. -5. -2. -16.
1. 2. 1. 8.
0. -1. 2. 4.
1. 0. - 12. - 36.
1. 2. 0. 5.
0. -1. 0. -2.
0. 0. - 12. - 36.
1. 0. 0. 1.
0. -1. 0. -2.
0. 0. - 12. - 36.
The Solution of Given Equations are :
x=1
y=2
z=3
Practical 4.b
Guass seidel Method
clc
clear
tol = 1e -4;
iter = 1;
maxit = 5;
//Intial guess
x =zeros(n ,1) ;
S =diag(diag(A));
T = S-A;
xold = x;
while(1)
for i = 1: n
x(i , iter +1) = (B(i) + T(i ,:) * xold ) / A(i ,i);
E(i , iter +1) = (x(i , iter +1) - xold (i))/x(i , iter +1)
*100; xold (i) = x(i , iter +1) ;
end
if x (: , iter ) == 0
E=1;
else
E =sqrt((sum(( E (: , iter +1) ).^2) )/n);
end
iter = iter + 1;
end
X = x (: , iter );
x =round(x *10^5) /10^5;
x (: ,1) = [];
Output :
Iteration No. x1 x2 x3
1. 1.2 1.06 0.948
2. 0.9992 1.00536 0.99909
3. 0.99956 1.00018 1.00005
4. 0.99998 1. 1.
5. 1. 1. 1.
Practical 6.A
Trapezoidal Method
clc;
clear;
//Value of y or f(x)
y =[1 0.9412 0.8 0.64 0.5];
/ Find h (interval)
h=x(2)-x(1) ;
for i =1 : n
if i ==1 | i == n then
area = area + y (i) // For first and last value directly add
else
area = area +2* y (i) // Remaining terms multiply by 2
end
end
output :
Value of Intergration By Trapezoidal Rule is = 0.782800
Practical 6.B
SimpSon’S 1/3 rd Method
clc;
clear;
//Value of y or f(x)
y =[1 0.9412 0.8 0.64 0.5];
/ Find h (interval)
h=x(2)-x(1) ;
end
/ By Simpson's 1/3rd formula multiply by (h/3)
area = area * (h/3);
Practical 6.C
Simpspns 3/8th Method
clc;
clear;
//Value of y or f(x)
y =[1 0.9412 0.8 0.64 0.5];
/ Find h (interval)
h=x(2)-x(1) ;
end
/ By Simpson's 3/8 rd formula multiply by (3h/8)
area = area * (3*h/8);
Output :
y(x=0.4)=
1.3106
Practical 7.B
EulEr’S modification mEthod
clc
clear
function [f]=dydt(x, y)
f = x + y; // Define the given
function endfunction
end
//Display Output
mprintf ("%5s %8s" , ' x ' , ' y ' )
disp ([( x0+h:h:x)' y])
output :
x y
0.1 1.1105
0.2 1.2432
Practical 7.C 1
R-K 2nd order method
clc
clear
/ Define the given function
function [f]=fun1(x, y)
f = (y/2)
+(3*x);
endfunction
output :
The value of y by RK 4 th order Method
y(0.25) = 1.2785
y(0.50) = 1.6013
Practical 8.A
Linear regression
clc
clear
//input the given value of x and y from table to matrix
form x = [20 30 35 40 45 50];
y = [10 11 11.8 12.4 13.5 14.4];
/ Define variable for
table X = x .^2;
Y = y;
n =length(Y);
/ write Required value in terms of matrix or eqation
/ Define L.H.S
M1 = [sum(Y);sum(X .* Y) ];
//Define R.H.S.
M2 = [n sum(X);sum(X) sum(X .^2) ];
//Solve Matrix or Equation
A=M2\M1;
//Give variable to Required Value
a =A(1);
b =A(2);
// Display by taking roundup the number
disp("Equation of regression can be expressed as Y = a + bX")
disp(round(a *10^4) /10^4 , "a =" )
disp(round(b *10^4) /10^4 , "b =" )
output :
Equation of regression can be expressed as Y = a + bX
a = 9.139
b = 0.0021
Practical 8.B
Polynomial regression
clc
clear
//input the given value of x and y from table to matrix form
X = 1:0.2:2;
Y = [0.98 1.4 1.86 2.55 2.28 3.2];
n =length(Y);
/ write Required value in terms of matrix or eqation
/ Define L.H.S
M1 = [sum(X .^2) sum(X .^3) sum(X .^4); sum(X) sum(X.^2)
sum(X .^3); n sum(X) sum(X .^2) ];
//Define R.H.S.
M2 = [sum(X .^2 .* Y);sum(X .* Y);sum(Y) ];