Keep getting an error with trapz. Please help

조회 수: 14 (최근 30일)
David
David 2023년 3월 16일
댓글: Torsten 2023년 3월 16일
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
p3 = 1×101
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
% Fourth Quarter
r4 = (3*t)/4:dt:t
r4 = 1×101
3.0000 3.0100 3.0200 3.0300 3.0400 3.0500 3.0600 3.0700 3.0800 3.0900 3.1000 3.1100 3.1200 3.1300 3.1400 3.1500 3.1600 3.1700 3.1800 3.1900 3.2000 3.2100 3.2200 3.2300 3.2400 3.2500 3.2600 3.2700 3.2800 3.2900
p4 = 0*r4;
time = [r1 r2 r3 r4]
time = 1×404
0 0.0100 0.0200 0.0300 0.0400 0.0500 0.0600 0.0700 0.0800 0.0900 0.1000 0.1100 0.1200 0.1300 0.1400 0.1500 0.1600 0.1700 0.1800 0.1900 0.2000 0.2100 0.2200 0.2300 0.2400 0.2500 0.2600 0.2700 0.2800 0.2900
gigi = [p1 p2 p3 p4]
gigi = 1×404
0 0.0314 0.0628 0.0941 0.1253 0.1564 0.1874 0.2181 0.2487 0.2790 0.3090 0.3387 0.3681 0.3971 0.4258 0.4540 0.4818 0.5090 0.5358 0.5621 0.5878 0.6129 0.6374 0.6613 0.6845 0.7071 0.7290 0.7501 0.7705 0.7902
figure(2)
plot(time,gigi,'r')
grid on
hold off
% Saif Bet
ribua = power(gigi,2)
ribua = 1×404
0 0.0010 0.0039 0.0089 0.0157 0.0245 0.0351 0.0476 0.0618 0.0778 0.0955 0.1147 0.1355 0.1577 0.1813 0.2061 0.2321 0.2591 0.2871 0.3159 0.3455 0.3757 0.4063 0.4373 0.4686 0.5000 0.5314 0.5627 0.5937 0.6243
baba = trapz(T,ribua)
Error using trapz
Point spacing must be a scalar specifying uniform spacing or a vector of x-coordinates for each data point.

답변 (1개)

Torsten
Torsten 2023년 3월 16일
baba = trapz(time,ribua)
instead of
baba = trapz(T,ribua)
  댓글 수: 2
David
David 2023년 3월 16일
편집: David 2023년 3월 16일
Thank you for fast response !
Will it integrate between 0 to 4 ?
The qeustion is will it include zero ?
And what does it mean the "time" ?
Thank you !
Torsten
Torsten 2023년 3월 16일
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)

댓글을 달려면 로그인하십시오.

카테고리

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