Quadratic integration coding error
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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
0 Kommentare
Antworten (2)
ChristianW
am 14 Mär. 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))
0 Kommentare
Youssef Khmou
am 14 Mär. 2013
Bearbeitet: Youssef Khmou
am 14 Mär. 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');
0 Kommentare
Siehe auch
Kategorien
Mehr zu Numerical Integration and Differentiation finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!