Untitled
unknown
plain_text
10 months ago
4.3 kB
5
Indexable
% ****** False Position Method ******** clc; clear all; close all; f=@(x) x*x*x - 4; a=3; b=2; e=0.0001; fprintf("Root : %f\n",f(a)); if f(a)*f(b)<0 for i=1:100 c=(a*f(b)-b*f(a))/(f(b)-f(a)); fprintf("Root = %d\n",c); if abs(f(c)) < e break; endif if f(a) * f(c) < 0 b=c; endif if f(b) * f(c) < 0 a = c; endif endfor else fprintf("There is no root"); end % *******************Newton Raphson Method****************** clc; clear all; close all; f = @(x) x^3 - 4; df = @(x) 3*x^2; x0 = 3; e = 0.0001; fprintf("Initial guess : %f\n",x0); for i = 1:100 x = x0 - (f(x0) / df(x0)); fprintf("Root = %f\n",x); if abs(x - x0) < e break; endif x0 = x; end % **************** Secant Method **************** f=@(x) x*x*x - 5*x+1; x0 = 0; x1 = 1; t_error = 0.00001; for i=2:100 x2 = x1-((x1-x0)*f(x1))/(f(x1)-f(x0)); fprintf("Root %d = %f\n",i,x2); if abs(x2-x1) < t_error break; endif x0=x1; x1=x2; endfor % **************** Elimination Method ************** A= input("Enter the co-efficient matrix:"); B= input("Enter the source matriux:"); N= length(B); X=zeros(N,1); AugMat=[A B] for j=1:N-1 for i=j+1 :N m= AugMat(i,j)/AugMat(j,j); AugMat(i,:)=AugMat(i,:)-m*AugMat(j,:); endfor end AugMat X(N)=AugMat(N,N+1)/AugMat(N,N); for k=N-1:-1:1 X(k)=(AugMat(k,N+1)-AugMat(k,k+1:N)*X(k+1:N))/AugMat(k,k); end AugMat % **************************Jordan Method*********************************** %jorden mathod %Ingrdients A = input('Enter your co-effcient matrix: ') B = input('Enter source matrix: ') N = length(B); %no of unknowns X = zeros(N,1); Aug = [A B] for j=1:N %for columns Aug(j,:) = Aug(j,:)/Aug(j,j) for i=1:N % for rows if i~=j m = Aug(i,j) Aug(i,:) = Aug(i,:) - m*Aug(j,:) end end end % *********Jordan Method Another Way************ function [x,err]=gauss_jordan_elim(A,b) A = [1 1 1;2 3 5; 4 0 5] % input for augmented matrix A b = [5 ; 8; 2] % intput for matrix B [n,m]=size(A); % finding the size of matrix A err =0; % calculation of error x=zeros(n,1); % calling fuction zero if n ~= m disp('error: n~=m'); % displaying error if found err = 1; end % end of the scope of if if length(b) ~= n % finding the legth of matrix B disp('error: wrong size of b'); % displaying error, if found err = 2; else if size(b,2) ~= 1 b=b'; end % end of the scope of if-else if size(b,2) ~= 1 disp('error: b is a matrix'); % displaying erron in matrix B err = 3; end end if err == 0 Aa=[A,b]; for i=1:n [Aa(i:n,i:n+1),err]=gauss_pivot(Aa(i:n,i:n+1)); if err == 0 Aa(1:n,i:n+1)=gauss_jordan_step(Aa(1:n,i:n+1),i); end end x=Aa(:,n+1); end A=0; function A1=gauss_jordan_step(A,i) % calling of fuction function [n,m]=size(A); % determination of size of matrix A A1=A; % assigning A to A1 s=A1(i,1); A1(i,:) = A(i,:)/s; k=[[1:i-1],[i+1:n]]; for j=k s=A1(j,1); A1(j,:)=A1(j,:)-A1(i,:)*s; end % end of for loop function [A1,err]=gauss_pivot(A) % calling of fucntion [n,m]=size(A); % finding the size of matrix A A1=A; % process of assigning err = 0; % error flag if A1(1,1) == 0 check = logical(1); % logical(1) - TRUE i = 1; while check i = i + 1; if i > n disp('error: matrix is singular'); err = 1; check = logical(0); else if A(i,1) ~= 0 & check check = logical(0); b=A1(i,:); % process to change row 1 to i A1(i,:)=A1(1,:); A1(1,:)=b; end end end end %*****************Seidal*********************** %seidal mathod A = input('Enter a Co-efficient Matrix A: '); B = input('Enter a source Matrix B (column vector): '); P = input('Enter initial Guess Vector: '); n = input('Enter no of iterations: '); e = input('Enter your tolerance: '); N = length(B); X = zeros(N,1); Y = zeros(N,1); for j=1:n for i=1:N X(i) = (B(i)/A(i,i)) - (A(i,[1:i-1,i+1:N])*P([1:i-1,i+1:N]))/A(i,i); end fprintf('Iteration no %d\n', j) disp(X) if norm(Y-X) < e fprintf('Convergence reached at iteration %d\n', j); break; end Y = X; end
Editor is loading...
Leave a Comment