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

8 views (last 30 days)
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)

Accepted Answer

Guillaume
Guillaume on 8 Feb 2016
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 Comment
Deen Halis
Deen Halis on 8 Feb 2016
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

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements 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!