PLEASE DO IN MATLAB. MUST BE IN MATLAB
Posted: Tue Jul 12, 2022 8:46 am
PLEASE DO IN MATLAB. MUST BE IN MATLAB
In this example, an m-file that calculates and plots the relative error associated with the composite trapezoidal rule is developed. The m-file, EX6A.m, developed in example 6A, is used here as a user-defined function called "trap". Function "trap" takes the number of segments, n, as inputs. The outputs are the value of the integral and the width, h. Note that the integration range [a, b] is defined as a "global" variable. In this case, the "global" statement appears both in the main program and in function "trap". Note also that the function to be integrated is also defined through a user-defined function. The main program of the m-file calculates the relative error € = | ² ~ Itrapezoid| For a range of values of the width, h. The number of segments is defined through a "logspace" statement. At the end, the relative error is plotted against the step size. As in example 6A, the m- file has been tested for I [ f(x) dx with f(x) = x³, that has an exact value of I = 0.25. The inputs are (see script file below): a = 0, b = 1, no = 1, n₁ = 6, m = 100. The script file,EX6A.m, is given in the next page.
clear ; global a b a = input ("Provide lower bound a: "); b = input ("Provide upper bound b: "); no=input('Provide no: ¹); n1=input('Provide n1: 1); m = input ("Provide no of log spaced points:'); p = logspace (no, n1,m); S0 = 0.25 ; for j = 1 : m n = round (p (j)); [s,h] = trap (n) ; x(j) = h; y(j) = abs((s0-=)/=0); fprintf( g 15.10f \n', h,s); end loglog (x, y, 'o--r', 'lineWidth', 2, 'MarkerSize',5) xlabel("Step Size') ylabel('Error') function [s,h] = trap (n) * composite trapezoidal rule global a b h = (b-a)/n ; x = a; s = 0.0 ; for m = 1 n-1 x = x+h; = = = + f(x) ; end s = 2.0-s ; s = = + f(a) + f(b) ; = = 0.5+h+s ; end function res = f(x) res = x^3; end
In this example, an m-file that calculates and plots the relative error associated with the composite trapezoidal rule is developed. The m-file, EX6A.m, developed in example 6A, is used here as a user-defined function called "trap". Function "trap" takes the number of segments, n, as inputs. The outputs are the value of the integral and the width, h. Note that the integration range [a, b] is defined as a "global" variable. In this case, the "global" statement appears both in the main program and in function "trap". Note also that the function to be integrated is also defined through a user-defined function. The main program of the m-file calculates the relative error € = | ² ~ Itrapezoid| For a range of values of the width, h. The number of segments is defined through a "logspace" statement. At the end, the relative error is plotted against the step size. As in example 6A, the m- file has been tested for I [ f(x) dx with f(x) = x³, that has an exact value of I = 0.25. The inputs are (see script file below): a = 0, b = 1, no = 1, n₁ = 6, m = 100. The script file,EX6A.m, is given in the next page.
clear ; global a b a = input ("Provide lower bound a: "); b = input ("Provide upper bound b: "); no=input('Provide no: ¹); n1=input('Provide n1: 1); m = input ("Provide no of log spaced points:'); p = logspace (no, n1,m); S0 = 0.25 ; for j = 1 : m n = round (p (j)); [s,h] = trap (n) ; x(j) = h; y(j) = abs((s0-=)/=0); fprintf( g 15.10f \n', h,s); end loglog (x, y, 'o--r', 'lineWidth', 2, 'MarkerSize',5) xlabel("Step Size') ylabel('Error') function [s,h] = trap (n) * composite trapezoidal rule global a b h = (b-a)/n ; x = a; s = 0.0 ; for m = 1 n-1 x = x+h; = = = + f(x) ; end s = 2.0-s ; s = = + f(a) + f(b) ; = = 0.5+h+s ; end function res = f(x) res = x^3; end