integration in matlab plot

11 次查看(过去 30 天)
I have a graph of 2 arrays, and I need to do an integration between 0 and 4.7. Can you please tell me how to do it?
x=[0 1.4660 2.9319 4.3979 5.8639 7.3298 8.7958 10.2618 11.7277 13.1937 14.6597]
y=[78.8093 78.8093 77.7206 76.6319 75.3877 73.2103 69.4776 64.2933 57.0353 44.3338 26.4480]

采纳的回答

Hassaan
Hassaan 2024-3-2
编辑:Hassaan 2024-3-2
% Your desired values here
x = [0, 0.5, 1.2, 1.4660, 2.9319, 4.3979, 5.8639, 7.3298, 8.7958, 10.2618, 11.7277, 13.1937, 14.6597];
y = [11.4, 22.90, 78.8093, 78.8093, 77.7206, 76.6319, 75.3877, 73.2103, 69.4776, 64.2933, 57.0353, 44.3338, 26.4480];
% Find the indices where x is less than or equal to 6
indices = find(x <= 6);
% Extract the subsets of x and y that are within the desired range
x_sub = x(indices);
y_sub = y(indices);
% Perform the integration on the subset
integralValue = trapz(x_sub, y_sub);
% Display the result
disp(['The integral value between 0 and 6 is: ', num2str(integralValue)]);
The integral value between 0 and 6 is: 404.4359
Learning Material:
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.
  2 个评论
Hassaan
Hassaan 2024-3-2
编辑:Hassaan 2024-3-2
Just for visualization purpose:
% Your desired values here
x = [0, 0.5, 1.2, 1.4660, 2.9319, 4.3979, 5.8639, 7.3298, 8.7958, 10.2618, 11.7277, 13.1937, 14.6597];
y = [11.4, 22.90, 78.8093, 78.8093, 77.7206, 76.6319, 75.3877, 73.2103, 69.4776, 64.2933, 57.0353, 44.3338, 26.4480];
% Plot the original data
figure;
plot(x, y, '-o', 'LineWidth', 2);
hold on;
% Highlight the integration area
% Find indices for x values within the desired range
indices = find(x <= 6);
% Use the fill function to highlight the area under the curve
x_fill = [x(indices), fliplr(x(indices))];
y_fill = [y(indices), zeros(1, length(indices))];
fill(x_fill, y_fill, 'cyan', 'LineStyle', 'none');
alpha(0.3); % Adjust transparency
% Annotate the graph
xlabel('X Axis');
ylabel('Y Axis');
title('Integration Area Visualization');
legend('Original Data', 'Integration Area', 'Location', 'NorthEast');
hold off;
Learning Material
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.
gravy
gravy 2024-3-2
you are the best, thanks!

请先登录,再进行评论。

更多回答(1 个)

John D'Errico
John D'Errico 2024-3-2
编辑:John D'Errico 2024-3-2
You CANNOT use trapz. Despite the fact that another poster has done so, that does not yield a result up to 4.7. If you have no data point that lies EXACTLY at x==4.7, trapz CANNOT be used.
x=[0 1.4660 2.9319 4.3979 5.8639 7.3298 8.7958 10.2618 11.7277 13.1937 14.6597];
y=[78.8093 78.8093 77.7206 76.6319 75.3877 73.2103 69.4776 64.2933 57.0353 44.3338 26.4480];
plot(x,y,'o-')
The solution is not that difficult though. I think fnint is part of the curve fitting toolbox, so you can do this:
fn = pchip(x,y);
Note that pchip is a better choice than spline here, since your curve seems to have a sharp transition in it in some cases.
fnI = fnint(fn)
fnI = struct with fields:
form: 'pp' breaks: [0 1.4660 2.9319 4.3979 5.8639 7.3298 8.7958 10.2618 11.7277 13.1937 14.6597] coefs: [10×5 double] pieces: 10 order: 5 dim: 1
fnval(fnI,4.7) - fnval(fnI,0)
ans = 366.6602
And that will be the integral from 0 to 4.7.
Another approach, if you lack the fnint function (as I said, part of the CFTB) is to just use integral.
integral(@(x) ppval(fn,x),0,4.7)
ans = 366.6602
As you can see, either yields the same result. However, trapz cannot do that.

类别

Help CenterFile Exchange 中查找有关 Numerical Integration and Differentiation 的更多信息

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by