# Creation of a .dat files in a for loop

2 views (last 30 days)
Richard Wood on 11 Mar 2020
Hello everyone,
I am trying to do the following. Let's imagine that you have the following arrays: mx_inelastic (30001x2434 elements), my_inelastic (30001x2434 elements), mz_inelastic (30001x2434 elements) and physical_time_inelastic (2434x1 elements). What I want to is to calculate is the first derivative with respect to time (with respect to the array physical_time_inelastic) of mx_inelastic, my_inelastic, and mz_inelastic, and then square each one and sum up them. To do this, I want to sum up all the the elements in each row of mx_inelastic, my_inelastic, and mz_inelastic, and then do the aforementioned time derivative. So at the end, on each time step I want to calculate dmdt=(d(mx_inelastic)/dt)^2+(d(my_inelastic)/dt)^2+(d(mz_inelastic)/dt)^2. For this, a for loop running over all the elements of physical_time_inelastic is neccesary. A possible problem is to calculate the temporal derivative in the first and last time step. Would there be any way to write it considering this? The first element of physical_time_inelastic is equal to zero. Moreover, I would want to create an array, squared_dmdt_inelastic (2434x1 elements) with the value of the aforementioned derivative on each step. It would be great if, at the end, I can have a .dat file of (2434x2 elements) where the first column is just physical_time_inelastic and the second column is squared_dmdt_inelastic. It would be great if all the digits generated by MatLab on the derivation process are present in the final .dat file.
Any idea on how I could face this problem?
Mohammad Sami on 12 Mar 2020
I want to sum up all the the elements in each row of mx_inelastic, my_inelastic, and mz_inelastic
Did you mean " sum each column first ?" If you sum the rows, you will get 30001 x 1 array which is incompatible with your time array of 2434 x 1 element

Mohammad Sami on 12 Mar 2020
% assume some values
mx_inelastic = rand(30001,2434);
my_inelastic = rand(30001,2434);
mz_inelastic = rand(30001,2434);
physical_time_inelastic = [1:2434]';
% sum the columns
s_mx_inelastic = sum(mx_inelastic);
s_my_inelastic = sum(my_inelastic);
s_mz_inelastic = sum(mz_inelastic);
% calculate the diff
d_s_mx_inelastic = diff(s_mx_inelastic);
d_s_my_inelastic = diff(s_my_inelastic);
d_s_mz_inelastic = diff(s_mz_inelastic);
dt = diff(physical_time_inelastic)';
dmdt = (d_s_mx_inelastic./dt).^2 + (d_s_my_inelastic./dt).^2 + (d_s_mz_inelastic./dt).^2