Implementing the Trapezoidal Method
4 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hi. The aim is to utilize Trapezoidal Method for an integral with no variables, so we will get the x after integration.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/960395/image.png)
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 Commenti
Torsten
il 11 Apr 2022
Modificato: Torsten
il 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)) ?
Risposte (1)
Ravi
il 21 Dic 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);
I hope this modification resolves the issue you are facing.
Thanks,
Ravi Chandra
0 Commenti
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!