Implementing the Trapezoidal Method

13 views (last 30 days)
Turgut Ataseven
Turgut Ataseven on 11 Apr 2022
Answered: Ravi on 21 Dec 2023
Hi. The aim is to utilize Trapezoidal Method for an integral with no variables, so we will get the x after integration.
where U is a constant number and u is a set of 37 experimental data. here is what I have tried:
input= [18.83183;18.85426;18.84941;18.81932;18.68937;18.35632;18.01761;17.59767;16.89197;16.18128;15.58579;14.55176;13.93881;12.94744;12.06294;11.14389;10.32951;9.8233;9.33477;8.77343;8.83133;8.52088;9.06752;9.56366;10.71528;11.46361;12.7156;13.63052;14.39822;15.24683;16.28031;17.07743;17.68488;18.46132;18.78772;18.87824;18.90421];
f=@ (x) (1-(input/18.73835 ).^2 );
a=-0.05; % Lower limit
b=0.05; % Upper limit
h=0.05; % Increment
n=(b-a)/h; % Number of subintervals
sum=0;
for k=1:1:n-1
x(k)=a+k*h;
y(k)=f(x(k)); %%%%%% line 31
sum=sum+y(k);
end
% Formula: (h/2)*[(y0+yn)+2*(y1+y2+y3+..+yn-1)]
result = h/2*(f(a)+f(b)+2*sum);
fprintf('\n Friction coefficient is: %f',result);
Error message:
Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.
Error in iki_cd (line 31)
y(k)=f(x(k));
I need your help to fix the code. Thanks.
  3 Comments
Turgut Ataseven
Turgut Ataseven on 11 Apr 2022
@Jan Thanks for the reply.
I want to calculate the result of the integral, which is 0.1*[1-u^2/U^2], and then put the "input" values to u one by one, thus get a single scalar value, "result".
Torsten
Torsten on 11 Apr 2022
Edited: Torsten on 11 Apr 2022
"u" in your integral is the array "input", I guess.
You have 37 array values - what are the corresponding x-values in the interval [-0.05:0.05] ?
Equidistant points
x = linspace(-0.05,0.05,37)
?
Then
result = trapz(x,1-(input/18.73835 ).^2)
But if "input" are velocities, should it be highest in the middle, i.e. at x=0 (input(19)) ?

Sign in to comment.

Answers (1)

Ravi
Ravi on 21 Dec 2023
Hi Turgut Ataseven,
I understand that you are facing an issue in implementing trapezoidal method for computing the numeric integration value.
I have noticed a small error in the code where you are computing the “f” function value. Instead of passing “x” as the parameter, “f” is taking “input” as parameter in computing the function. On replacing “input” with “x” you would obtain the desired results.
Please find the below modified code.
input= [18.83183;18.85426;18.84941;18.81932;18.68937;18.35632;18.01761;17.59767;16.89197;16.18128;15.58579;14.55176;13.93881;12.94744;12.06294;11.14389;10.32951;9.8233;9.33477;8.77343;8.83133;8.52088;9.06752;9.56366;10.71528;11.46361;12.7156;13.63052;14.39822;15.24683;16.28031;17.07743;17.68488;18.46132;18.78772;18.87824;18.90421];
f=@ (x) (1-(x/18.73835 ).^2 );
a=-0.05; % Lower limit
b=0.05; % Upper limit
h=0.05; % Increment
n=(b-a)/h; % Number of subintervals
sum=0;
for k=1:1:n-1
x(k)=a+k*h;
y(k)=f(x(k)); %%%%%% line 31
sum=sum+y(k);
end
% Formula: (h/2)*[(y0+yn)+2*(y1+y2+y3+..+yn-1)]
result = h/2*(f(a)+f(b)+2*sum);
fprintf('\n Friction coefficient is: %f',result);
Friction coefficient is: 0.100000
I hope this modification resolves the issue you are facing.
Thanks,
Ravi Chandra

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!