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

1 visualización (últimos 30 días)
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)

Respuesta aceptada

Guillaume
Guillaume el 8 de Feb. de 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 comentario
Deen Halis
Deen Halis el 8 de Feb. de 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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by