Mid Square Method Code implementation in C and MatLab:
Problem:
Mid square method, mid square random number generator Code in MatLab and C or C++.
Views:
Maniruzzaman Akash, A programmer, A web programmer, a helpful person to share knowledge and everything..
%Function f = @(x) 5 * x^4 - 2.7 * x^2 - 2*x + 0.5; l = input('Enter Lower Limit : '); u = input('Enter Upper Limit : '); previous_approximation = 0; f_l = feval(f, l); f_u = feval(f, u); root = u -((f_u * (l-u))/ (f_l - f_u)); given_absoulte_error = input('Enter absolute error : '); absolute_error = abs((root - previous_approximation) / root ) * 100; previous_approximation = root; while(absolute_error > given_absoulte_error) f_l = feval(f, l); f_u = feval(f, u); f_root = feval(f, root); if(f_l * f_root > 0) l = root; else u = root; end; f_l = feval(f, l); f_u = feval(f, u); root = u -((f_u * (l-u))/ (f_l - f_u)); absolute_error = abs((root - previous_approximation) / root ) * 100; previous_approximation = root; fprintf('Root %f Error %f\n', root, absolute_error); end; disp('-------------------------------'); fprintf('Final Root = %f and absolute error = %f', root, absolute_error);
f = @(x) x^2 - 3; l = input('Enter Lower Limit : '); u = input('Enter Upper Limit : '); prev_approximation = 0; root = (l + u)/2; f_root = feval(f, root); given_absoulte_error = input('Enter absolute error : '); absolute_error = abs((root - prev_approximation) / root) * 100; disp(absolute_error); fprintf('\nStep a\tb\tf(a)\tf(b)\troot\tError_abs\n'); while(absolute_error > given_absoulte_error) f_root = feval(f, root); f_l = feval(f, l); f_u = feval(f, u); if(f_l * f_root > 0) l = root; else u = root; end; root = (l + u)/2; absolute_error = abs((root - prev_approximation) / root) * 100; prev_approximation = root; fprintf('Step %f Error %f\n', root, absolute_error); fprintf('\nStep a\tb\tf(a)\tf(b)\troot\tError_abs\n'); end; disp('-------------------------------'); fprintf('Final Root = %f and absolute error = %f', root, absolute_error);
f = @ (x) 0.2+25*x-200*x^2+675*x^3-900*x^4+400*x^5; n = input('Enter how many parts do you want? : '); a = input('Enter lower limit : '); b = input('Enter Upper Limit : '); part = (b - a)/n; array = [0,0,0,0,0,0,0,0,0,0,0]; for i=1:n if(i == 1) array(i) = a; else array(i) = array(i-1) + part; end; end; result = 0; for i=1:n+1 x = i-1; if(x==0 || x == n) result = result + feval(f,array(i)); end if(x~=0 || x~=n) if(rem(x,2)==0) result = result + 2*feval(f,array(i)); else result = result + 4*feval(f,array(i)); end end end I = (b-a) * (result /(3 * n)); fprintf('\nIntegral is : %f\n', I); exact_integral = 1.640533; E = abs((exact_integral - I)/exact_integral) * 100; fprintf('\nTrue Error = %f\n', E);
f = @ (x) 0.2 + 25*x - 200*(x^2) + 675*(x^3) -900*(x^4) + 400*(x^5);
f = @ (x) 0.2 + 25*x - 200*(x^2) + 675*(x^3) -900*(x^4) + 400*(x^5); n = input('Enter how many parts do you want? : '); a = input('Enter lower limit : '); b = input('Enter Upper Limit : '); part = (b - a)/n; array = [0,0,0,0,0,0,0,0,0,0,0]; if(n == 2) array(1) = a; array(2) = b; else for i=1:n if(i == 1) array(i) = a; else array(i) = array(i-1) + part; end; end; end; result = 0; for i=1:n+1 x = i-1; if(x==0 || x == n) result = result + feval(f,array(i)); end if(x~=0 || x~=n) result = result + 2*feval(f,array(i)); end end I = (result / 2*n) * (b - a); fprintf('Integral is = %f\n', I); exact_integral = 1.640533; E = abs((exact_integral - I)/exact_integral) * 100; fprintf('\nTrue Error = %f\n', E);
f = @ (x) 0.2 + 25*x - 200*(x^2) + 675*(x^3) -900*(x^4) + 400*(x^5); a = input('Enter lower limit : '); b = input('Enter upper limit limit : '); x0 = a; x1 = (b-a)/3; x2 = 2 * x1; x3 = b; fx0 = feval(f, x0); fx1 = feval(f, x1); fx2 = feval(f, x2); fx3 = feval(f, x3); I = (b - a) * ((fx0 + 3*(fx1 + fx2) + fx3)/8); exact_integral = input('Enter Exact Integral : '); E = abs((exact_integral - I)/exact_integral) * 100; fprintf('\nIntegral = %f\n', I); fprintf('\nTrue Error = %f\n', E);----------------------------------------
% Author : Maniruzzaman Akash % Code : Simpsons 1/3 Rules Code in Matlab Implementation % Function f = @ (x) .2+ 25*x - 200*x^2+675*x^3-900*x^4+400*x^5; disp('Press 1 for set values '); disp('Press 2 for see results '); disp('Press 0 for exit the program '); disp('---------------------------------'); isInputGiven = false; while(1) choose = input('Give your choose option: '); if(choose == 1) a = input('Enter lower limit : '); b = input('Enter upper limit : '); exact_integral = input('Enter exact integral : '); x0 = a; x1 = (b-a)/2; x2 = b; fx0 = feval(f,x0); fx1 = feval(f,x1); fx2 = feval(f,x2); integral = ((fx0 + (4*fx1) + fx2)/6 * (b-a)); isInputGiven = true; end; if(choose == 2) if(isInputGiven == true) integral_output = sprintf('Integral is = %0.5f',integral); disp(integral_output); exact_error = ((exact_integral - integral)/exact_integral) * 100 ; exact_error_output = sprintf('Exact Error = %0.5f',exact_error); disp(exact_error_output); end; end; if(choose == 0) disp('Exiting the code..'); exit(0); end; end;
![]() |
Simpson's 1/3 rules Code implementation in MatLab |