cumsum function question for integration

44 ビュー (過去 30 日間)
haohaoxuexi1
haohaoxuexi1 2022 年 9 月 21 日
編集済み: Torsten 2022 年 9 月 21 日
The code is below.
close all;
clear all;
clc;
format long;
syms t;
inter_val = 0.0001;
tn=0:inter_val:20;
t=tn;
y_cos = sin(t);
int_y_cos=cumsum(y_cos*inter_val);
figure(1);
plot(t, y_cos, 'k');
box on; grid on;
figure(2);
plot(t, int_y_cos, 'r');
box on; grid on;
I want to know why figure(2) didn't come out with cos plot but with offset?
But switch y_cos = sin(t) to y_cos = cos(t), it can give a perfect sin plot?
Thanks,

採用された回答

Torsten
Torsten 2022 年 9 月 21 日
編集済み: Torsten 2022 年 9 月 21 日
cumsum(y_cos*inter_val);
is a Riemann sum for the integral of sin(t) - integrated between 0 and x.
But the integral of sin(t) from 0 to x is 1-cos(x), not cos(x).

その他の回答 (1 件)

Cris LaPierre
Cris LaPierre 2022 年 9 月 21 日
It has to do with the fact that sin(t) starts at 0 and is positive until pi. cumsum is just adding up the Y values, so it oscillates between 0 and 2. With cos(t), it is only positive for pi/2, so cumsum only gets to half the value before it starts decreasing, or 1. It then is negative for pi, decreasing the value by 2, reaching -1. It therefore oscillates between -1 and 1.

カテゴリ

Help Center および File ExchangeFunction Creation についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by