I need this question answered in Matlab. Please show your code, that is the only
ID: 3709461 • Letter: I
Question
I need this question answered in Matlab. Please show your code, that is the only way this question can be answered correctly. The second photo is a pseudo code that shoiuld be followed thanks. Also follow directions closely. Thanks
QUESTION 1: (Problem 19.5) The function f(x) = e-x can be used to generate the following table of unequally spaced data: x 0 0.1 0.3 0.5 0.7 0.95 1.2 f(x) 0.9048 0.7408 0.6065 0.4966 0.3867 0.3012 Evaluate the integral from a = 0 to b= 0.6 using (a) thc trapezoidal rule, compute the true percent relative error Integral using built-in function: built in function syms x Itrue-int (exp(-),a,b)i % built in function % func- @ (x) exp (-?); OR Itrue integral (func, a, b) Trapczoidal rule (cqually spaced segments): where h-(b-a)n; n numher of segments Trapczoidal rule (unequally spaced segments) f(x)+f(x2) f(xn-l) + f(%) Simpson's 13d rule: f(to) + 4f(xl)+f(x2) -[f(xo) + 4f(xi ) + f(x2)] 1(b-a) OR Where »-(b-an; a x0, b-x2, and x1- the point midway betwcen a and b, which is given by (a t b)2: number of segments. Simpson's 3/8th rule: OR 3h where h= (b-a)/3.Explanation / Answer
PLEASE REFER BELOW CODE
1) CREATE trapuneq.m AND PASTE BELOW CODE
function I = trapuneq(x,y)
if nargin < 2
error('At least 2 input arguments required');
end
if any(diff(x) < 0)
error('X not monotonically acending');
end
n = length(x);
if length(y) ~= n
error('x and y must be same length');
end
s = 0;
for k = 1:n-1
s = s + (x(k+1) - x(k)) * (y(k) + y(k+1))/2;
end
I = s;
2) CREATE test.m AND PASTE BELOW CODE
close all
clear all
clc
format short;
syms x
a = 0;
b = 0.6;
Itrue = int(exp(-x),a,b);
fprintf('Itrue = %f ', Itrue );
x = [0 0.1 0.3 0.5 0.7 0.95 1.2];
y = [1 0.9048 0.7408 0.6065 0.4966 0.3867 0.3012];
Icalculate = trapuneq(x,y);
fprintf('Icalculate = %f ', Icalculate );
relative_err = ((Icalculate - Itrue) / Icalculate);
fprintf('relative_err = %f ', relative_err );
RUN test.m AND YOU WILL GET BELOW OUTPUT
Itrue = 0.451188
Icalculate = 0.701240
relative_err = 0.356585
>>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.