PLEASE DO IN MATLAB. MUST BE IN MATLAB

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

PLEASE DO IN MATLAB. MUST BE IN MATLAB

Post by answerhappygod »

PLEASE DO IN MATLAB. MUST BE IN MATLAB
Please Do In Matlab Must Be In Matlab 1
Please Do In Matlab Must Be In Matlab 1 (95.88 KiB) Viewed 31 times
Please Do In Matlab Must Be In Matlab 2
Please Do In Matlab Must Be In Matlab 2 (35.69 KiB) Viewed 31 times
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
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply