Assignment (Final Lab)
Assignment (Final Lab)
: : :
S.No.
Topics
Page No.
1 2 3 4 5 6 7 8 9 10 11 12 13
To use Subplot in your Program. Using for Loop, write code for computing factorial of given integer. Determine Fibonacci Series. To determine Percentage and Grade of Students. Find Sinx by Recursion Method. Find the nth Term, Complete Series and Sum of Arithmetic Series. Find the nth Term, Complete Series and Sum of Geometric Series. Make a program of Bisection Method. Determine Newton Raphson Method. To determine Secant Method To determine Regular Falsi Method. Make a program of Gauss Elimination Method. Make a program of Echelon Form. Make a program to find a solution of linear Equations by the use of LU Decomposition. Make a program of Gauss Seidal Method. Make a program of Newton Forward Difference Method. Make a program of Newton Backward Difference Method. Make a program of Newton Forward Divided Method. Make a program of Newton Backward Divided Method. Make a program of Lagrange Polynomial interpolation.
1 4 7 10 14 17 20 23 26 29 32 35 38
14
41
15 16 17 18 19 20
44 47 50 53 56 59
Page 3 of 64
Question#1
To use Subplot in your Program.
Algorithm:
1. Initialize variable x & y i.e. 0 to 20 with step size of 0.1 & sinx respectively. 2. Make subplot of two rows and two columns and select first one then plot y with respect to x in red color. 3. Assign cosx in y and then select second subplot and plot y with respect to x in steric (*) form. 4. Select third subplot and again plot y with respect to x in (o) form. 5. Now select forth subplot and plot y with respect to x in steric (*) form and hold this to make a plot combine with previous plot. Then assign sinx in y again and plot this.
Page 4 of 64
Flowchart:
Start
x 0 : 0.1 : 20 Y sinx
Subplot(2,2,1) Plot(x,y,r)
Subplot(2,2,3) Plot(x,y,o)
Subplot(2,2,4) Plot(x,y,*)
Hold On ysinx
Plot(x,y,h)
Stop
Page 5 of 64
Source Code:
x = 0: 0.1 : 20; y = sin(x); subplot(2,2,1); plot(x,y,'r'); y = cos(x); subplot(2,2,2); plot(x,y,'*'); subplot(2,2,3); plot(x,y,'o'); subplot(2,2,4); plot(x,y,'*'); hold on; y = sin(x); plot(x,y,'h');
PLOT:
Page 6 of 64
Question#02
Using for Loop, write code for computing factorial of given integer.
Algorithm
1. 2. 3. 4. 5. 6. This Program computes factorial of given integer with the help of for loop. Declare fact=1; Ask user to enter the number for factorial. Multiply fact with num and stored in fact again (i.e. fact=fact*num) Increment variable num by 1. Repeat steps 4 and 5 until num is less than or equal to the number that the user entered otherwise go to step 7. 7. Quit the loop and display factorial of required value i.e. fact. 8. Quit the program.
Page 7 of 64
Flowchart
Start
fact=1
Input n
fact=fact*num
Output fact
End
Page 8 of 64
Source Code
fact = 1; n = input ('Enter Factorial Number'); for num = 1 : n fact = fact * num; end fprintf ('The Factorial is %d', fact);
Output
Enter Factorial Number = 5 The Factorial is = 120
Page 9 of 64
Question#03
Determine Fibonacci Series.
Algorithm
1) 2) 3) 4) 5) This Program determines Fibonacci Series. Take input nth Term, First Term and Second term. Add First and Second Term into Third Term. Save Second value into first value then save third value into Second value and then display. Repeat steps 3 & 4 until nth Terms.
Page 10 of 64
Flowchart
Start
Do i = 3 : nth
End
Page 11 of 64
Source Code
n = input('\nEnter nth Term = '); First_Term = input('Enter First Term = '); Second_Term = input('Enter Second Term = '); fprintf('%d + ',First_Term); fprintf('%d + ',Second_Term); for i = 3 : n Third_Term = First_Term + Second_Term; First_Term = Second_Term; Second_Term = Third_Term; fprintf('%d + ',Third_Term); end
Output
Enter nth Term = 6 Enter First Term = 1 Enter Second Term = 2 1 + 2 + 3 + 5 + 8 + 13 + >>
Page 12 of 64
Question#04
To determine Percentage and Grade of Students.
Algorithm
1) This Program determines percentage of Students of five subject having same total marks. 2) Declare and initialize sum = 0. 3) Input marks and add this marks into sum five times. 4) Calculate percentage using
Page 13 of 64
Flowchart
Start
Sum 0
Do i = 1 to 5
Input marks
per >= 80
per >= 70
per >= 60
per >= 50
per >= 40
Display Grade A+
Display Grade A
Display Grade B
Display Grade C
Display Grade D
Display Fail
End
Page 14 of 64
Source Code
sum = 0; for i=1:5 fprintf('Enter Marks of %u = ',i); marks = input(''); sum = sum + marks; end per = (sum/500) * 100; fprintf('\nPercentage is %0.2f\n', per); if( per >= 80 ) fprintf('Your Grade is A+ :)'); else if(per >= 70) fprintf('Your Grade is A'); else if(per >= 60) fprintf('Your Grade is B'); else if(per >= 50) fprintf('Your Grade is C'); else if(per >= 40) fprintf('Your Grade is D'); else fprintf('You are Fail :('); end end end end end
Page 15 of 64
Output
Enter Marks of 1 = 90 Enter Marks of 2 = 89 Enter Marks of 3 = 78 Enter Marks of 4 = 98 Enter Marks of 5 = 98
Page 16 of 64
Question#05
Find Sinx by Recursion Method.
Algorithm
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. This Program determine Sinx by Recursion Formula Read any angle & tolerance Convert degree into Radian Initialize n =1, T(n) =1 and S(n) = T(n) Increment in n Do T(n) = ((-x.^2)*T(n-1))/((2*n -1)*(2*n - 2)) & S(n) = S(n-1) + T(n) Repeat from step 5 until T(n) >tol Dispay S(n) Quit the program.
Page 17 of 64
Flowchart
Start
Tolerance = 0.001
x = degree * pi / 180
No
Display S(n)
End
Page 18 of 64
Source Code
% Recursion Form of Sinx
degree = input('Enter any angle = '); tol = input('Enter Tolerance = '); x = degree * pi / 180; n = 1; T(n) = x; S(n) = T(n); while(abs(T(n)) > tol) n=n+1; T(n) = ((-x.^2)*T(n-1))/((2*n -1)*(2*n - 2)); S(n) = S(n-1) + T(n); end fprintf('Sin(%d) = %0.3f\n',degree,S(n)); fprintf('Actual is %0.3f',sin(x));
Page 19 of 64
Question#06
Find the nth Term, Complete Series and Sum of Arithmetic Series.
Algorithm
1. 2. 3. 4. This Program finds the nth Term and Sum of Arithmetic Series. Take Input values of a(First Term of Series), nth (Number of Term) & d(Common Difference) Determine each term and Sum of series until nth Terms and save each in an array. Display complete Series, nth Term & Sum of nth Term.
Page 20 of 64
Flowchart
Start
Input a, d & n
End
Page 21 of 64
Source Code
% Airthmatic Progression of nth Terms
a = input('\nEnter First Term '); d = input('Enter Difference d '); n = input('Enter nth Term u want to know '); fprintf(' The Series is\n '); for i = 1 : n T(i) = a + ( i - 1)*d; S(i) = 1/2 *i*(a + T(i)); fprintf(' %d +',T(i)); end fprintf('\n T(%u) is %u\n',n,T(n)); fprintf(' S(%u)is %u\n',n,S(n));
Page 22 of 64
Question#07
Find the nth Term, Complete Series and Sum of Geometric Series.
Algorithm
1) 2) 3) 4) This Program finds the nth Term and Sum of Geometric Series. Take Input values of a(First Term of Series), nth (Number of Term) & r(common ratio). Determine each term and Sum of series until nth Terms and save each in an array. Display complete Series, nth Term & Sum of nth Term.
Page 23 of 64
Flowchart
Start
Input a, r & n
T(i) = a * r^(i-1)
S(i) = a * (1-r^i)/(1-r)
End
Page 24 of 64
Source Code
%Geometric Progression of nth Terms
a = input('Enter First Term '); r = input('Enter Common ratio '); n = input('Enter nth Term u want to know '); fprintf('The Series is\n '); for i = 1 : n T(i) = a * r^(i-1); S(i) = a * (1 - r^i)/(1 - r); fprintf(' %0.2f +',T(i)); end fprintf('\n T(%u)is %0.2f\n',n,T(n)); fprintf(' S(%u)is %0.2f\n',n,S(n));
Page 25 of 64
Question#8
Make a program of Bisection Method.
Algorithm
1. 2. 3. 4. 5. 6. 7. 8. This Program gives Bisection Method. Input values a, b, function & tolerance. Check if f(a).f(b) is positive than go back to step1 else Find c = (a+b)/2. If f (a).f(c) is negative then do b = c else set a = c. Compute error If error is greater than tolerance than repeat from step4 Otherwise end.
Page 26 of 64
Flowchart
Start
No f(a).f(b)<0
Yes
c = (a+b)/2
No
b=c
a=c
Compute error
End
Page 27 of 64
Source Code
clc error = 0; f1 = input('Enter any function','s'); f = inline(f1); while(1) a = input('Enter value of a'); b = input('Enter value of b'); tol = input('Enter Tolerence'); if( f(a)*f(b) < 0) break; end end fprintf('S.NO a b c(Midpoint) f(a) f(b) f(c) error\n'); serialno = 1; while(1) c = (a+b)/2; fprintf(' %d %0.3f %0.3f %0.3f %0.3f %0.3f %0.3f %0.4f\n', serialno,a,b,c,f(a),f(b),f(c),abs(error)); if(f(c)*f(a) < 0) b = c; else a = c; end serialno = serialno +1 ; new = (a+b)/2; error = c - new; if(abs(error)< tol) break; end end
Output
Enter any functionx^3 - 16 Enter value of a2 Enter value of b3 Enter Tolerence0.01 S.NO a b c(Midpoint) f(a) 1 2.000 3.000 2.500 -8.000 2 2.500 3.000 2.750 -0.375 3 2.500 2.750 2.625 -0.375 4 2.500 2.625 2.563 -0.375 5 2.500 2.563 2.531 -0.375 6 2.500 2.531 2.516 -0.375 >>
f(c) error -0.375 0.0000 4.797 0.2500 2.088 0.1250 0.826 0.0625 0.218 0.0313 -0.080 0.0156
Page 28 of 64
Question#9
Determine Newton Raphson Method.
Algorithm
1. 2. 3. 4. This Program determines Newton Raphson Method. Input any function and tolerance, take a derivative of function. Initialize variable i = 1, error = 0 & r(i) = 1. Compute r(i+1) by formula i.e.
r(i+1) = ((- f1(r(i))/f2(r(i))) + r(i))
5. Display r(i+1) and do increment in i. 6. Compute error and compare it with tolerance that if error is greater than tolerance then repeat from step 4 otherwise end the program.
Page 29 of 64
Flowchart
Start
Display r(i+1)
Inc i
Page 30 of 64
Source Code
clc syms x y; f11 = input('Enter function = ','s'); tol = input('Enter tolerance = '); f22 = diff(f11,x); f1 = inline(f11); f2 = inline(f22); i = 1; error=0; r(i) = 1; fprintf('i xi xi+1 Error\n'); while(1) r(i+1) = ((- f1(r(i))/f2(r(i))) + r(i)); fprintf('%d %0.3f %0.3f %0.3f \n',i,r(i),r(i+1),error); i = i+1; error = r(i) - r(i+1); if(error <= tol) break; end end
Output
Enter function = x^3 - 16 Enter tolerance = 0.0003 i xi xi+1 1 1.000 6.000 2 6.000 4.148 3 4.148 3.075 4 3.075 2.614 5 2.614 2.523 6 2.523 2.520 Error 0.000 1.852 1.073 0.461 0.091 0.003
Page 31 of 64
Question#10
To determine Secant Method.
Algorithm
1. 2. 3. 4. This Program determines Secant Method. Input any function and tolerance value. Initialize i = 2, r(1) = 2 & r(2) = 2. Compute r(i+1) by formula i.e.
r(i+1) = r(i) - f(r(i))* ((r(i) - r(i-1))/(f(r(i)) - f(r(i-1))))
5. Display r(i+1) and do increment in i. 6. Compare if error is greater than tolerance than repeat from step 4 otherwise end the program.
Page 32 of 64
Flowchart
Start
Display r(i+1)
Inc i
Page 33 of 64
Source Code
clc f1 = input('Enter any Function = ','s'); tol = input('Enter any Tolerance = '); f = inline(f1); i = 2; r(1) = 1; r(2) = 2; fprintf('i r(i) r(i-1) f(r(i)) f(r(i-1)) r(i+1))\n'); while(1) r(i+1) = r(i) - f(r(i))* ((r(i) - r(i-1))/(f(r(i)) - f(r(i-1)))); fprintf('%d %0.3f %0.3f %0.3f %0.3f %0.3f\n',i,r(i),r(i-1),f(r(i)),f(r(i-1)),r(i+1)); i = i+1; if(abs(f(r(i+1))) <= tol) break; end end
Output
Enter any Function = exp(x) - 2 * x - x ^ 2
2 2.000 1.000 -0.611 -0.282 0.144 3 0.144 2.000 0.846 -0.611 1.222 4 1.222 0.144 -0.543 0.846 0.800
Page 34 of 64
Question#11
To determine Regular Falsi Method.
Algorithm
1. 2. 3. 4. 5. 6. 7. 8. This Program gives Regular Falsi Method. Input values a, b, function & tolerance. Check if f(a).f(b) is positive than go back to step1 else Find c = b-((f(b)*(b-a))/(f(b)-f(a))). If f (a).f(c) is negative then do b = c else set a = c. Compute error If error is greater than tolerance than repeat from step4 Otherwise end.
Page 35 of 64
Flowchart
Start
No f(a).f(b)<0
No
b=c
a=c
Compute error
End
Page 36 of 64
Source Code
clc error = 0; f1 = input('Enter any function = ','s'); f = inline(f1); while(1) a = input('Enter value of a'); b = input('Enter value of b'); tol = input('Enter Tolerence'); if( f(a)*f(b) < 0) break; end end fprintf('S.NO a b c(Midpoint) f(a) f(b) f(c) error\n'); serialno = 1; while(1) c = b-((f(b)*(b-a))/(f(b)-f(a))); fprintf(' %d %0.3f %0.3f %0.3f %0.3f %0.3f %0.3f %0.4f\n', serialno,a,b,c,f(a),f(b),f(c),abs(error)); if(f(c)*f(a) < 0) b = c; else a = c; end serialno = serialno +1 ; new = b-((f(b)*(b-a))/(f(b)-f(a))); error = c - new; if(abs(error)< tol) break; end end
Output
Enter any function = x^2 - 16 Enter value of a2 Enter value of b6 Enter Tolerence0.001 S.NO a b c(Midpoint) f(a) f(b) f(c) error 1 2.000 6.000 3.500 -12.000 20.000 -3.750 0.0000 2 3.500 6.000 3.895 -3.750 20.000 -0.831 0.3947 3 3.895 6.000 3.979 -0.831 20.000 -0.170 0.0840 4 3.979 6.000 3.996 -0.170 20.000 -0.034 0.0170 5 3.996 6.000 3.999 -0.034 20.000 -0.007 0.0034
Page 37 of 64
Question#12
Make a program of Gauss Elimination Method.
Algorithm
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. This Program determines Gauss Elimination Method. Input any Matrix. Save size of matrix in separate variables (e.g. m = row & n = column). Initialize i = 1, do Ri = Ri / aii . Initialize j = i+1, then do Rj,i = Rj,i aj,i * Ri until j = m. Repeat step 4 until i = m. Do ym = am,m+1 Initialize j = m-1 . Initialize sum = 0 & i = j+1. Do decrement in j by 1. Do sum = sum + aj,i *yi until i = 1. Do yj = aj,m+1 - sum and display it Repeat from step 9 until j = 1. Terminate the program
Page 38 of 64
Flowchart
Start Input Matrix
Do $ j = m-1 to 1 decrement in j by 1
Do j = i+1 to m
sum = sum+X(j,i)*y(i)
Display y(j)
y(m) = X(m,m+1);
End
Page 39 of 64
Source Code
clc X = input('Enter any Matrix = '); [m,n] = size(X); for i = 1 : m X(i,1:n) = X(i,1:n) ./ X(i,i) ; for j = i+1 : m X(j,1:n) = X(j,1:n) - X(j,i) .* X(i,1:n); end end y(m) = X(m,m+1); fprintf('y(%d) = %0.3f\n',m,y(m)); for j = m-1:-1:1 sum = 0; for i = j+1:m sum = sum+X(j,i)*y(i); end y(j) = X(j,m+1) - sum; fprintf('y(%d) = %0.3f\n',j,y(j)); end
Output
Enter any Matrix = [3,4,5,8;6,3,2,6;9,8,4,2] y(3) = 3.043 y(2) = -2.870 y(1) = 1.420 >>
Page 40 of 64
Question#13
Make a program of Echelon Form.
Algorithm
1. 2. 3. 4. 5. 6. This Program determines Echelon Form. Input any Matrix. Save size of matrix in separate variables (e.g. m = row & n = column). Initialize i = 1, do Ri = Ri / aii . Initialize j = i+1, then do Rj,i = Rj,i aj,i * Ri until j = m. Repeat step 4 until i = m.
Page 41 of 64
Flowchart
Start
Input Matrix
m = Rows n = Column
Do i = 1 to m
Do j = i+1 to m
Display Matrix
End
Page 42 of 64
Source Code
clc X = input('Enter any Matrix = '); [m,n] = size(X); for i = 1 : m X(i,1:n) = X(i,1:n) ./ X(i,i) ; for j = i+1 : m X(j,1:n) = X(j,1:n) - X(j,i) .* X(i,1:n); end end
Output
Enter any Matrix = [3,4,5,8;6,3,2,6;9,8,4,2] >> X X= 1.0000 1.3333 1.6667 2.6667 0 1.0000 1.6000 2.0000 0 0 1.0000 3.0435 >>
Page 43 of 64
Question#14
Make a program to find a solution of linear Equations by the use of LU Decomposition.
Algorithm
1. 2. 3. 4. 5. This program finds a solution of linear Equations by the use of LU Decomposition. Input Matrix A & B and Augmented both in Matrix M. Save Row and column in n & m respectively. Initialize U as an identity matrix and L of zeroes matrix of dimension of M matrix. Do l(:,1)=M(:,1) u(1,2:n)=M(1,2:n)/l(1,1) Initialize i = 2 Initialize j = i Sum = 0 Initialize k = 1 Do sum=sum + l (j, k)*u (k, i) with decrement in k, until k is equal to i 1. Do l(j, i) = M(j, i)-sum, with increment in j, repeat from step8 until j will equal to n Initialize j = i+1 Sum = 0 Initialize k = 1 Do sum=sum + l (i, k)*u (k,j) with decrement in k, until k is equal to i 1. Do u(i, j) =( M(i, j)-sum)/ l(i,i), with increment in j, repeat from step13 until j will equal to m Do xn = un,m Initialize j = n-1 . Initialize sum = 0 & i = j+1. Do sum = sum + uj,i *xi until i = n. Do xj = uj,m - sum and display it Repeat from step 19 until j = 1. Terminate the program.
6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23.
Page 44 of 64
Flowchart
Start
@
sum=sum+l(j,k)*u( k,i)
x(n) = u(n,m)
M Augment A & B
Do $ j = n-1 to 1 decrement in j by 1
Sum 0
u = eye(n, m) l = zeroes(n, m)
Do j=i+1 to m
Do i = j+1 to n
Sum = 0
sum = sum+u(j,i)*x(i)
Do i=2 to n
Do k=1 to i-1
Do j=i to n
sum=sum+l(i,k)*u( k,j)
Sum = 0
Display x(j)
Do k=1 to i-1
u(i,j)=(M(i,j)-sum)/ l(i,i)
End
Page 45 of 64
Source Code
Clc Clear all A=input('Enter any Matrix'); B=input('Enter Column matrix'); M=[A,B]; [n,m]=size(M); u=eye(n,m); l=zeros(n,m); l(:,1)=M(:,1); u(1,2:n)=M(1,2:n)/l(1,1); for i=2:n for j=i:n sum=0; for k=1:i-1 sum=sum+l(j,k)*u(k,i); end l(j,i)=M(j,i)-sum; end for j=i+1:m sum=0; for k=1:i-1 sum=sum+l(i,k)*u(k,j); end u(i,j)=(M(i,j)-sum)/l(i,i); end end x(n)=u(n,m); fprintf('value of x(%u) is %0.2f \n',n,x(n)); for j=n-1:-1:1 sum=0; for i=j+1:n sum=sum+u(j,i)*x(i); end x(j)=u(j,m)-sum; fprintf('value of x(%u) is %0.2f \n',j,x(j)); end
Output
Enter any Matrix[4,6,7;3,6,3;9,2,8] Enter Column matrix[6;2;8] value of x(3) is -0.93 value of x(2) is -0.07 value of x(1) is 1.73 >>
Page 46 of 64
Question#15
Make a program of Gauss Seidal Method.
Algorithm
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. This program Determines Gauss Seidal Method. Input Matrix A, B & tolerance. Save rows and columns in n & m respectively. Initialize X0 = [0 0 0] Initialize k = 1 Initialize i= 1 Sum = 0 Initialize j = 1 Do sum = sum + A (i, j)*X0(1, j), if j is not equal to i then increment in j until j = n. Do Xn (i) = (B(i) - sum)/A(i, i) error(i) = abs(Xn(i) - X0(i)) X0(i) = Xn (i) Repeat from step 7, until i = n. Compute error and check if error is less than tolerance than end the program otherwise repeat from step 6 until k = (maximum iterations)
Page 47 of 64
Flowchart
Start
nrows mcolumns
Set X0 = Zeros(1,n)
Do K = 1 to 50
Do i = 1 to n
Sum 0
e = max(error)
ENd
True
Page 48 of 64
Source Code
clc clear all A = input('Enter First Matrix'); B = input('Enter Second Matrix'); tol = input('Enter Tolerance'); [n,m] = size(A); X0 = zeros(1,n); for k =1:50 for i = 1:n sum = 0; for j=1 :n if(j~=i) sum = sum + A(i,j)*X0(1,j); end end Xn(i) = (B(i) - sum)/A(i,i); error(i) = abs(Xn(i) - X0(i)); X0(i) = Xn(i); end Xn e = max(error); if(e<tol) break; end end
Page 49 of 64
Question#16
Make a program of Newton Forward Difference Method.
Algorithm:
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15.
16. 17.
This Program determined Newton Forward Difference Method. Take an input x, y values & the value of x you want to estimate. save size of row or column in variable say n initiate i = 1 & do D(i,1) = (y(i+1) - y(i)) until i = n-1 initiate j = 2 initiate i = 1 & do D(i,j) = (D(i+1,j-1) - D(i,j-1)) Until i = n-j Repeat step 6 until j = n-1 Compute u = (X x(1))/(x(2) x(1)) Initiate sum = y(1), prd = 1 & i = 1 Do
prd = prd * (u (i-1)) sum = sum + (D(1,i)*prd)/factorial(i)
Until i = n-1 18. Display value of sum and quit the program.
Page 50 of 64
Source Code
clc clear all x = input('Enter values of x = '); y = input('Enter values of y = '); X = input('Enter value of x you want to estimate = '); n = length(x); for i = 1 : n-1 D(i,1) = y(i+1) - y(i); end for j = 2 : n-1 for i = 1 : n-j D(i,j) = D(i+1,j-1) - D(i,j-1); end end u = (X - x(1))/(x(2) - x(1)); sum = y(1); prd = 1; for i = 1 : n-1 prd = (prd * (u-(i-1))); sum = sum + (prd*D(1,i))/factorial(i); end fprintf('The value of y is %.3f at x %0.3f',sum,X);
OUTPUT:
Enter values of x = [.2 .3 .4 .5 .6] Enter values of y = [.2304 .2788 .3222 .3617 .3979] Enter value of x you want to estimate = .36 The value of y is 0.3054 at x 0.360>>
Page 51 of 64
FLOWCHART
Start
Do @ i = 1 to n-1
u = (X - x(1))/(x(2) - x(1))
@ Do j = 1 to n-1
Do j = 2 to n-1 prd = prd *(u-(i-1)) sum = sum + (prd*D(1,i))/ factorial(i) Do i =1 to n-j D(i,j) = D(i+1,j-1) D(i,j-1) Display sum
End
Page 52 of 64
Question#17
Make a program of Newton Backward Difference Method.
Algorithm:
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. This Program determined Newton Backward Difference Method. Take an input x, y values & the value of x you want to estimate. save size of row or column in variable say n initiate i = 2 & do D(i,1) = (y(i)-y(i-1)) until i = n initiate j = 2 initiate i = j+1 & do D(i,1) = (y(i)-y(i-1)) Until i = n Repeat step 6 until j = n-1 Compute u = (X-X(n))/(x(2)-x(1)) Initiate sum = y(n), prd = 1 & j = 1 Do prd = prd * (u + (i-1)) sum = sum + (D(n,i)*prd)/factorial(i) 17. Until j = n-1 18. Display value of sum and quit the program.
Page 53 of 64
Source Code
clc clear all x = input('Enter values of x = '); y = input('Enter values of y = '); X = input('Enter value of x you want to estimate = '); n = length(x); for i = 2 : n D(i,1) = y(i) - y(i-1); end for j = 2 : n-1 for i = j+1 : n D(i,j) = D(i,j-1) - D(i-1,j-1); end end u = (X - x(n))/(x(2) - x(1)); sum = y(n); prd = 1; for i = 1 : n-1 prd = (prd * (u+(i-1))); sum = sum + (prd*D(n,i))/factorial(i); end fprintf('The value of y is %.4f at x %0.3f',sum,X);
OUTPUT:
Enter values of x = [1 3 5 7 9] Enter values of y = [23 45 67 89 32] Enter value of x you want to estimate = 3.5 The value of y is 49.1499 at x 3.500>>
Page 54 of 64
FLOWCHART
Start
n length (x)
Do @ i = 2 to n
u = (x-x(n))/(x(2)x(1))
Do % j = 1 to n-1
Do j = 2 to n-1
Do i = j+1 to n %
End
Page 55 of 64
Question#18
Make a program of Newton Forward Divided Method.
Algorithm:
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14.
15. 16.
This Program determined Newton Forward Divided Method. Take an input x, y values & the value of x you want to estimate. save size of row or column in variable say n initiate i = 1 & do D(i,1) = (y(i+1) - y(i))/(x(i+1) - x(i)) until i = n-1 initiate j = 2 initiate i = 1 & do D(i,j) = (D(i+1,j-1) - D(i,j-1))/(x(i+j) - x(i)) Until i = n-j Repeat step 6 until j = n-1 Initiate sum = y(1), prd = 1 & i = 1 Do
prd = prd * (X - x(i)) sum = sum + D(1,i)*prd
Until i = n-1 17. Display value of sum and quit the program.
Page 56 of 64
Source Code
clc clear all x = input('Enter values of x = '); y = input('Enter values of y = '); X = input('Enter value of x you want to estimate = '); n = length(x); for i = 1 : n-1 D(i,1) = (y(i+1) - y(i))/(x(i+1) - x(i)); end for j = 2 : n-1 for i = 1 : n-j D(i,j) = (D(i+1,j-1) - D(i,j-1))/(x(i+j) - x(i)); end end sum = y(1); prd = 1; for i = 1 : n-1 prd = prd * (X - x(i)); sum = sum + D(1,i)*prd; end fprintf('The value of y is %0.3f at x %0.3f',sum,X);
OUTPUT:
Enter values of x = [1 2 4 6 7 9] Enter values of y = [1 16 256 1296 2401 6561] Enter value of x you want to estimate = 1.3 The value of y is 2.856 at x 1.300>>
Page 57 of 64
FLOWCHART
Start
n length (x)
Do j = 1 to n-1
Do @ i = 1 to n-1
Page 58 of 64
Question#19
Make a program of Newton Backward Divided Method.
Algorithm:
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. This Program determined Newton Backward Divided Method. Take an input x, y values & the value of x you want to estimate. save size of row or column in variable say n initiate i = 2 & do D(i,1) = (y(i)-y(i-1))/(x(i) - x(i-1)) until i = n initiate j = 2 initiate i = j+1 & do D(i,1) = (y(i)-y(i-1))/(x(i) - x(i-1)); Until i = n Repeat step 6 until j = n-1 Initiate sum = y(n), prd = 1 & j = n Do prd = prd * (X - x(j)) sum = sum + D(n,n(j-1))*prd decrease j by one Until i = 2 Display value of sum and quit the program.
Page 59 of 64
Source Code
clc clear all x = input('Enter values of x = '); y = input('Enter values of y = '); n = length(x); for i = 2 : n D(i,1) = (y(i)-y(i-1))/(x(i) - x(i-1)); end for j = 2:n-1 for i = j+1:n D(i,j) = (D(i,j-1) - D(i-1,j-1))/(x(i) - x(i-j)); end end X = input('Enter value of X = '); sum = y(n); prd = 1; for j = n :-1:2 prd = prd * (X-x(j)); sum = sum + D(n,n-(j-1))*prd; end fprintf('The value of y is %0.3f at x %0.3f',sum,X);
OUTPUT:
Enter values of x = [1 2 4 6 7 9]
Enter values of y = [1 16 256 1296 2401 6561] Enter value of X = 8 The value of y is 4096.000 at x 8.000>>
Page 60 of 64
FLOWCHART
Start
n length (x)
Do @ i = 2 to n
Do @ j = n to 2
Do % j = 2 to n-1 @
Page 61 of 64
Question
Make a program of Lagrange Polynomial interpolation.
Algorithm
1. 2. 3. 4. 5. This Program determines Lagrange Polynomial interpolation. Read n (no of terms), x, x1, x2, xn & y1, y2, yn. Initialize sum = 0 & k = 1 Initialize product = 1, i = 1 Do Product = product * ((X - x (i))/(x (k) - x (i))), if i is not equal to k other wise repeat step 5 with increment in i until i will be equal to n 6. Do Sum = sum + (y (k) * product), and repeat from step 4 with increment in k, until k equal to n. 7. Display y at value of given x. 8. Terminate the program.
Page 62 of 64
Flowchart
Start
nEnter No of terms i != k True Read x, x1,x2 xn & y1,y2...yn product product * ((X - x(i))/(x(k) x(i)))
False
Sum 0
Do k = 1 to n
Product = 1
Do i = 1 to n
Display y at value of x
End
Page 63 of 64
Source Code
clc clear all n =input('Enter No of terms you want to = '); for r = 1:n fprintf('Enter X(%u) = ',r); x(r) = input(''); fprintf('Enter y(%u) = ',r); y(r) = input(''); end fprintf(' x y \n'); for r = 1 : n fprintf('%0.2f %0.2f\n',x(r),y(r)); end X = input('Enter value of x at which you want to estimate the value of y = '); sum = 0 ; for k = 1 : n product = 1; for i = 1 : n if(i~=k) product = product * ((X - x(i))/(x(k) - x(i))); end end sum = sum + (y(k) * product); end fprintf(' y is "%.2f" at value of x "%.2f"',sum,X);
Page 64 of 64
Output
Enter No of terms you want = 4 Enter X(1) = 1 Enter y(1) = 4 Enter X(2) = 2 Enter y(2) = 6 Enter X(3) = 3 Enter y(3) = 8 Enter X(4) = 4 Enter y(4) = 10 x 1.00 2.00 3.00 4.00 y 4.00 6.00 8.00 10.00
Enter value of x at which you want to estimate the value of y = 2.9 y is "7.80" at value of x "2.90">>