Can anyone help fix my code? I am trying to get my output to be the same as the
ID: 3282800 • Letter: C
Question
Can anyone help fix my code? I am trying to get my output to be the same as the example 1 but i cannot get my loop correct.
My MATLAB code
a = input('Enter lower limit: ');
b = input('Enter upper limit: ');
n = input('Enter number of sub-intervals: ');
%Trapezoidal Rule
T = a:n:b;
Width = ((b-a)/n);
F = Width/n;
C = a + Width;
D = (1/n)*Width*(a+b);
i=1;
while n>0 && i<(n)
E = (a + Width);
Y = (sqrt(1+(E)^2));
i = i+1;
end
Trap = D + (n*E);
disp(Trap)
Output:
Enter lower limit: 1
Enter upper limit: 5
Enter number of sub-intervals: 8
12.3750
1.4142
5.0990
1.8028
2.0790
y1 = (sqrt(1+a^2));
yn = (sqrt(1+b^2));
i=1;
while n>0 && i < n
Y = (sqrt(1+(a+Width)^2));
i=i+1;
end
disp(y1)
disp(yn)
disp(Y)
Trap2 = (Width/2)*(y1+yn+Y);
disp(Trap2)
SOLVED EXAMPLES OF TRAPEZOIDAL RULE Example 1: Use the trapezoidal rule with n = 8 to estimate: Solution: Given, For n-8, we have, ?:?= 0.5 we compute the values of yo, yi,y2. . . . ,Ys 11.5 22.533.54 4.5 5 10 325 17 2123 Therefore, todr 0.5 12.76
Explanation / Answer
simple matlab code
%%%%
clc;
clear all;
close all;
%%% function definition
f=@(x) sqrt(1+x^2);
a=input('Enter lower limit : ');
b=input('Enter Upper limit : ');
n=input('Enter number of subinterval : ');
h=(b-a)/n;
t=a:h:b;
sum=0;
for k=2:length(t)-1
sum=sum+2*feval(f,t(k));
end
i=h/2*(feval(f,a)+feval(f,b)+sum);
fprintf('Trepezoidal Integartion value : %f ',i);
OUTPUT:
Enter lower limit : 1
Enter Upper limit : 5
Enter number of subinterval : 8
Trepezoidal Integartion value : 12.761627
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.