How do I perform this operation using for loop? or any other approach?

1 回表示 (過去 30 日間)
Deen Halis
Deen Halis 2016 年 2 月 8 日
コメント済み: Deen Halis 2016 年 2 月 8 日
Hello, I'm trying to do approximate integration of this function using different time steps. I need to get the results of FF1 for each of the values of c in one operation. Thanks in advance
clc
clear
w = 20; %exciting/forcing frequency
p = 15;
T1 = 2*pi/w; %exciting/forcing period
n = 3*T1;
q0 = 10;
G = 0.05;
% c = 10:10:100; % need to get values of FF1 for this range of values;
c = 20; %this is one value of c;
ts = T1/c; %time step
t = 0:ts:n;
qt1 = q0*sin(w*t);
F1 = ts*qt1.*sin(p*t).*exp(G/2*p*t);
FF1 = cumsum(F1)

採用された回答

Guillaume
Guillaume 2016 年 2 月 8 日
I'm not sure where the difficulty lies for you. As you say in your question, simply use a for loop. You probably want to store each FF1 for each value of c. Since the number of elements in FF1 varies with c, you have to store them in a cell array. So:
%constant declarations
%...
timesteps = 10:10:100
FF1 = cell(1, timesteps); %preallocate array for speed
for tidx = 1:numel(timesteps)
c = timesteps(tidx);
ts = T1/c; %time step
t = 0:ts:n;
qt1 = q0*sin(w*t);
F1 = ts*qt1.*sin(p*t).*exp(G/2*p*t);
FF1{tidx} = cumsum(F1)
end
  1 件のコメント
Deen Halis
Deen Halis 2016 年 2 月 8 日
Thanks Guillaume, I initially had empty cells with your code, but made some little changes and it's now ok. I sat through yesterday and couldn't get around this, had errors all the time. I appreciate your quick response.
%....
timesteps = A:B:L;
FF1 = cell(1, L); %preallocate array for speed
for tidx = 1:numel(timesteps)
c = timesteps(tidx);
ts = T1/c; %time step
t = 0:ts:n;
qt1 = q0*sin(w*t);
F1 = ts*qt1.*sin(p*t).*exp(G/2*p*t);
% FF1{tidx} = cumsum(F1);
FFa = cumsum(F1)
end

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by