How to save the data every time i run 3 For loops?

1 visualización (últimos 30 días)
Nikhil Murali Krishnaa
Nikhil Murali Krishnaa el 3 de Jun. de 2019
Comentada: madhan ravi el 3 de Jun. de 2019
I run a Mass-spring-damper system and i want to calculate percentage error of all combinations. Then find the value with the minimum error. But i could not save the data . It only saves the one of the last
for K1 = 170000:5000:210000
for B1 = 0:10:100
for M1 = 1:0.05:1.2
sim('MSDsystem');
Display=ans.Displacement(:,2);
delta=abs(Display-Mass1(:,2));
percentagediff=delta./Mass1(:,2);
meanpctdiff=mean(percentagediff);
mat(K1,:)=meanpctdiff
end
end
end

Respuesta aceptada

madhan ravi
madhan ravi el 3 de Jun. de 2019
K1 = 170000:5000:210000;
B1 = 0:10:100;
M1 = 1:0.05:1.2;
kn = numel(K1);
bn = numel(B1);
mn = numel(M1);
mat=zeros(kn,bn,mn); % preallocate
for k1 = 1:kn
for b1 = 1:bn
for m1 = 1:mn
S=sim('MSDsystem');
Display=S.Displacement(:,2);
delta=abs(Display-Mass1(:,2));
percentagediff=delta./Mass1(:,2);
meanpctdiff=mean(percentagediff);
mat(k1,b1,m1)=meanpctdiff;
end
end
end
  2 comentarios
Nikhil Murali Krishnaa
Nikhil Murali Krishnaa el 3 de Jun. de 2019
but now the the variables inside simulink does not change. How do i do that?
madhan ravi
madhan ravi el 3 de Jun. de 2019
I’m not full fledged in Simulink sorry.

Iniciar sesión para comentar.

Más respuestas (1)

KSSV
KSSV el 3 de Jun. de 2019
for K1 = 170000:5000:210000
for B1 = 0:10:100
for M1 = 1:0.05:1.2
sim('MSDsystem');
Display=ans.Displacement(:,2);
delta=abs(Display-Mass1(:,2));
percentagediff=delta./Mass1(:,2);
meanpctdiff=mean(percentagediff);
mat(K1,B1,M1)=meanpctdiff
end
end
end
It is suggested to pre-allocate mat before loops.
  2 comentarios
Nikhil Murali Krishnaa
Nikhil Murali Krishnaa el 3 de Jun. de 2019
It is showing an error as "Index in position 2 is invalid. Array indices must be positive integers or logical values."
Nikhil Murali Krishnaa
Nikhil Murali Krishnaa el 3 de Jun. de 2019
The problem is it is taking the value K1 has the array size. But the loop runs only 495 times

Iniciar sesión para comentar.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by