Quadratic integration coding error

3 vues (au cours des 30 derniers jours)
Raye
Raye le 14 Mar 2013
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

Réponses (2)

ChristianW
ChristianW le 14 Mar 2013
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 le 14 Mar 2013
Modifié(e) : Youssef Khmou le 14 Mar 2013
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');

Catégories

En savoir plus sur Numerical Integration and Differentiation dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by