Effacer les filtres
Effacer les filtres

Can someone please explain why the variables I, Area, Mx, My has an incorrect values? Thanks!

4 vues (au cours des 30 derniers jours)
%Define the symbolic Variable x
syms x
%Enter the upper and lower functions as y1(x) and y2(x), respectively.
y1(x) = x^2-5;
k=randi(20,1);
y2(x) = k*x;
%Solve the point of intersections. Save the array of solutions as roots.
roots = solve(y1(x) == y2(x));
%Save the lower value as x1 and the higher limit as x2. Use min and max to compare the roots for lower and upper limit.
%Convert the roots from symbolic constants to double.
x1 = double(min(roots));
x2 = double(max(roots));
%Find the points of intersection between the twop curves. Set it as an array P1 = [x, y]
P1 = [x1, y1(x1)];
P2 = [x2, y2(x2)];
%Set the difference of the upper minus the lower function as I. Use absolute value function to ensure positive area
I = symfun(int(y1(x) - y2(x), x, x1, x2), x);
%Use the integration to the find the area bounded by the two curves at the computed boundary.
Area = int(y1(x) - y2(x), x, x1, x2);
%Determine the moment along y axis.
My = int((1/2)*(y1(x)^2 - y2(x)^2), x, x1, x2);
%Determine the centroidal element ybar by finding the midoint of the lower and upper curves.
ybar = double(My/I);
%Determine the moment along y axis.
Mx = int(x*(y1(x) - y2(x)), x, x1, x2);
%Find the centroidal elements xc,yc by dividing the Moments by the Area. Convert the answer as Doubles
xc = double(Mx/I);
yc = double(ybar);
%Combine the centroidal elements as Centroid as an array of element xc and yc
Centroid = [xc, yc];
%Set the graphing margin allowance to 1
margin = 1;
%Graphing of the regions
ezplot(y1,[x1-margin, x2+margin]);
hold on;
ezplot(y2,[x1-margin,x2+margin]);
grid on;
k= linspace(x1,x2);
k2 = [k,fliplr(k)];
inBetween =[y1(k), fliplr(y2(k))];
fill(k2, inBetween, 'y');
plot(x1,y1(x1),"r*" );
plot(x2,y1(x2),"r*" );
title ("Centroid of Plane Region")
plot(xc,yc,"bo")
  3 commentaires
Gabriel Enzo
Gabriel Enzo le 10 Avr 2023
All the values were generated randomly using the randi... function in MATLAB. Therefore, the values of k, x1, x2, I, Area, My, Mx, xc, yc, P1, and P2 would all vary each time the code is run, depending on the randomly generated values of k and the intersection points of the two functions y1(x) and y2(x). Yet errors were the variables of I, Area, Mx, My.
Dyuman Joshi
Dyuman Joshi le 10 Avr 2023
There does not seem to be any error in your code.

Connectez-vous pour commenter.

Réponse acceptée

Gokul Nath S J
Gokul Nath S J le 19 Avr 2023
Hi Gabriel,
Based on my understanding, it seems that you would like to enquire why the are incorrect. Eventhough the values appear similar, note that there is a slight error in the area and I calcuation. You didn't compute the absolute value of the area and I. Hence they are expressed as negative. So you might have to take the absolute value of the area.
Further for defining y2, you have used a random variable which will change the value of k each time you run the code. So you may expect different value of area if you are running the code multiple times.
Thanks,
Gokul Nath S J

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by