2 views (last 30 days)
Raye on 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)
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

ChristianW on 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)

Youssef Khmou on 14 Mar 2013
Edited: Youssef Khmou on 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=trapz(x,y1);
a2=trapz(x,y2);
a3=trapz(x,y3);
% FIGURE
figure, plot([c c2 c3],[A1 A2 A3])
hold on, plot([c c2 c3],[a1 a2 a3],'r')
hold off, xlabel(' constants ci'),
ylabel(' Integrals magnitude');