Quadratic integration coding error

조회 수: 2 (최근 30일)
Raye
Raye 2013년 3월 14일
The code I have done as shown below is the trapezoidal integration of the functions. I am having some an issue of the quad function to accept the function y to be accepted.
if true
% code
x = t;
t = 0:0.1:10;
for c= 0.1;
y1 = t.*sin(c.*t);
A= trapz(x,y1)
a= quad(@y,0,10)
for c2 = 10*pi;
y2= t.*sin(c2.*t);
A2 = trapz(x,y2)
for c3 = 200
y3= t.*sin(c3*t);
A3 = trapz(x,y3)
end
end
end
end

답변 (2개)

ChristianW
ChristianW 2013년 3월 14일
The trapz inputs are just points. But quad needs a function as input.
x = 0:1:10;
y = sin(x);
A_trapz = trapz(x,y)
f = @(z) sin(z); % y = f(z) = sin(z)
A_quad = quad(f,x(1),x(end))

Youssef  Khmou
Youssef Khmou 2013년 3월 14일
편집: Youssef Khmou 2013년 3월 14일
hi,
like the answer above, quad takes function handle as input, while trapz accepts vectors , but the way you wrote the loops is incorrect , beside you do not need loops :
% RANGE
t = 0:0.1:10;
x = t;
%Constants
c= 0.1;
c2 = 10*pi;
c3 = 200;
%1) Function Handle and vector
Y1=@(t) t.*sin(c.*t);
y1= t.*sin(c.*t);
%2) Function Handle and vector
Y2=@(t) t.*sin(c2.*t);
y2= t.*sin(c2.*t);
%3) Function Handle and vector
Y3=@(t) t.*sin(c3.*t);
y3= t.*sin(c3.*t);
%INTEGRALS
A1=quad(Y1,0,10);
a1=trapz(x,y1);
A2=quad(Y2,0,10);
a2=trapz(x,y2);
A3=quad(Y3,0,10);
a3=trapz(x,y3);
% FIGURE
figure, plot([c c2 c3],[A1 A2 A3])
hold on, plot([c c2 c3],[a1 a2 a3],'r')
legend(' USING QUAD','USING TRAPZ')
hold off, xlabel(' constants ci'),
ylabel(' Integrals magnitude');

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by