Keep getting an error with trapz. Please help
Show older comments
clear
close all
clc
dt = 0.01;
t = 4;
T = 0:dt:t;
% First Quarter
r1 = 0:dt:t/4 ;
p1 = sin(pi.*r1) ;
hold on
figure(1)
% Second Quarter
r2 = t/4:dt:t/2;
p2 = 0*r2;
% Third Quearter
r3 = t/2:dt:(3*t)/4;
p3 = 1.^r3
% Fourth Quarter
r4 = (3*t)/4:dt:t
p4 = 0*r4;
time = [r1 r2 r3 r4]
gigi = [p1 p2 p3 p4]
figure(2)
plot(time,gigi,'r')
grid on
hold off
% Saif Bet
ribua = power(gigi,2)
baba = trapz(T,ribua)
Answers (1)
Torsten
on 16 Mar 2023
baba = trapz(time,ribua)
instead of
baba = trapz(T,ribua)
2 Comments
Torsten
on 16 Mar 2023
r1 goes from 0 to 1, r2 goes from 1 to 2, r3 goes from 2 to 3, r4 goes from 3 to 4.
The function is defined piecewise in p1, p2, p3 and p4 corresponding to the r1, r2, r3 and r4.
Thus the integral of the function over [0 4] is
baba = trapz(r1,p1.^2) + trapz(r2,p2.^2) + trapz(r3,p3.^2) + trapz(r4,p4.^2)
which equals
baba = trapz(time,ribua)
Categories
Find more on Numerical Integration and Differentiation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
