How to graph 'trapz' function

조회 수: 21 (최근 30일)
Max Payne
Max Payne 2019년 8월 5일
답변: Divyajyoti Nayak 2024년 9월 5일
I am using a trapz function to estimate the area under a function, and am getting the answer, but I was wondering if there was any way to graph the trapeziums that the function is using to calculate the area, rather than just getting an answer down the bottom.
Side note: does anyone know why my ylim is giving an error of "Array indices must be positive integers or logical values"?
Thanks!
x=0:0.1:6;
y=(30-(exp(-2.3*x)*30))/(0.6*57400)*100-(0.015*x)
plot(x,y)
ylim ([0 0.07])
Q = trapz(x,y)
  댓글 수: 1
Walter Roberson
Walter Roberson 2019년 8월 5일
Check, you might have accidentally created a variable named ylim

댓글을 달려면 로그인하십시오.

답변 (1개)

Divyajyoti Nayak
Divyajyoti Nayak 2024년 9월 5일
To visualize the trapeziums used for calculation by the 'trapz' function, you can make use of the 'xline' and 'area' functions like below:
x=0:0.1:6;
y=(30-(exp(-2.3*x)*30))/(0.6*57400)*100-(0.015*x)
plot(x,y)
ylim ([0 0.07])
Q = trapz(x,y)
hold on
xline(x);
a = area(x,y);
a.FaceColor = [0.3010 0.7450 0.9330];
hold off
It would look something like this:

카테고리

Help CenterFile Exchange에서 Numerical Integration and Differentiation에 대해 자세히 알아보기

태그

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by