Trapezoid Rule method errors

I am trying to do a similar thing to my other question but using trapz command instead. can someone explain what I have done wrong.
function Trapezoid_Rule(~,~)
uavg = input('average velocity m/s ');
R= input('inner radius m ');
syms x r
T = uavg.*pi.*R.^2;
B = (1-(r./R).^2).*r;
BI = trapz(x,trapz(r,B,2));
disp(BI)%print the values in the command promt
end
Error using trapz (line 47)
Dimension argument must be a positive integer scalar within indexing range.
Error in coursework>Trapezoid_Rule (line 131)
BI = trapz(x,trapz(r,B,2));
Error while evaluating UIControl Callback.

댓글 수: 4

darova
darova 2021년 3월 14일
x and r should be numeric vectors
Coolman
Coolman 2021년 3월 14일
I am new to Matlab what command does that. please
r = linspace(0,R,100);
x = linspace(0,2*pi,100);
Coolman
Coolman 2021년 3월 14일
Error using trapz (line 47)
Dimension argument must be a positive integer scalar within indexing range.
Error in coursework>Trapezoid_Rule (line 132)
BI = trapz(x,trapz(r,B,2));
Error while evaluating UIControl Callback.
gets to B even generating a series of numbers and then BI crashes with above error

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

답변 (1개)

Uday Pradhan
Uday Pradhan 2021년 3월 19일

0 개 추천

Hi,
The error you have posted occurs because the integral:
trapz(r,B,2)
evaluates to 0. This is then used as the dimension argument while evaluating the exterior integral, hence the error.
The function "trapz" is to be used to numerically approximate the integral of a function over a certain interval.
With the help of the documentation, I have made some changes that lets you find the approximate inegral of B over the domain mentioned in your question. Hope it will help you!
uavg = input('average velocity m/s ');
R= input('inner radius m ');
%syms x r
r = 0:0.1:R;
theta = 0:0.1:(2*pi);
[R1,Theta] = meshgrid(r,theta);
T = uavg.*pi.*R.^2;
B = (1-(R1./R).^2).*R1;
%BI = trapz(x,trapz(r,B,2));
BI = trapz(theta,trapz(r,B,2));

카테고리

도움말 센터File Exchange에서 Numerical Integration and Differentiation에 대해 자세히 알아보기

제품

태그

질문:

2021년 3월 14일

답변:

2021년 3월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by