Summation with for LOOP

7 ビュー (過去 30 日間)
Kabir Shariff
Kabir Shariff 2022 年 5 月 18 日
コメント済み: Mathieu NOE 2022 年 5 月 18 日
Hello,
I have a function y that is repeated after a certain interval of T. I want to add the value of the previous y to the next value. The proplem is more clear with a diagram as shown below
I want to add the value of the each T to like,
How can I sumUp all the inteaction with a for Loop,like this expression?
here is my code for info
clear
clc
x = linspace(0,50,1000);
v = 10;
a = 0.2254; b = 0.5401;
xT = 0;
for i = 1:4
xT = xT+v;
y = @(x) ((a*((x-xT)).^-b).*(x>=(xT+1))).^2;
figure(3)
hold on
plot(x,y(x))
end
Merci

採用された回答

Mathieu NOE
Mathieu NOE 2022 年 5 月 18 日
hello
a simple add on to your code :
clear
clc
x = linspace(0,50,1000);
v = 10;
a = 0.2254; b = 0.5401;
xT = 0;
ysum = 0;
for i = 1:4
xT = xT+v;
y = @(x) ((a*((x-xT)).^-b).*(x>=(xT+1))).^2;
ysum = ysum + y(x);
figure(3)
hold on
plot(x,y(x))
end
plot(x,ysum,'--')
  2 件のコメント
Kabir Shariff
Kabir Shariff 2022 年 5 月 18 日
Thank you, It works!
could you explain to ysum please.
I see its a matrix, My initial idea was to use just the last value at the next T
Mathieu NOE
Mathieu NOE 2022 年 5 月 18 日
my pleasure
the concept is quite simple
at each loop iteration the new y data (y(x)) is summed with the previous one util the nd of the loop. It's doable because every y data comes from the same x data , so you can simply do this y summation and plot it against x;

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by