# calculation of average and difference from imported data

7 views (last 30 days)
Victor Albert on 21 Aug 2020
Commented: Rena Berman on 12 Oct 2020 at 17:07
Am looking at plotting the graph for P vs exposure time, P vs stress, and P vs time vs stress(all in one graph), and finally calculate the cumulative value for P as captured in the equation 1 below
The values for stress and exposure time is read from a .xy file format ( i atatched it here as a .txt file) with two columns and a long list of rows. Let’s say 2000 rows with different values for time and stress at every point.
The expression for D is captured below
P = F x stress^a + time^b….. ………..(1)
Where
a, has a constant value of 2.416
b has a constant value of 0.785
And F with a constant value of 3.62 x 10-7
Please note: The values of (stress) and t (exposure time) is read from a file. But the computation required to get the exact value of stress and time from the read file is
current value for shear stress=(current row+previous row)/2------------------(2)
current value for exposure time=current time-previous time=∆t---------(-3)
Put the calculated values for each current value of time and stress to calculate the D values based on equation 1
please how do i go about implementing this in matlab. i have imported the attached diument in MATLAB, seperating this variables and implementing the above equations for 2 and 3 for this sets of values is a challenge. can any one guide me.

Rik on 22 Aug 2020
Where are you getting that stress value from? Or those time values? Not from the file you attached to your question.
This doesn't need to be this hard. Write a proper description of your data, or attach your actual data. You can also attach a mat file if you prefer, but this isn't working.
Rik on 26 Aug 2020
Deleted question and comments posted by Victor Albert as far as I was able to retrieve it from Google cache (WBM link to cache):
calculation of average and difference from imported data
Am looking at plotting the graph for P vs exposure time, P vs stress, and P vs time vs stress(all in one graph), and finally calculate the cumulative value for P as captured in the equation 1 below
The values for stress and exposure time is read from a .xy file format ( i atatched it here as a .txt file) with two columns and a long list of rows. Let’s say 2000 rows with different values for time and stress at every point.
The expression for D is captured below
P = F x stress^a + time^b….. ………..(1)
Where
a, has a constant value of 2.416
b has a constant value of 0.785
And F with a constant value of 3.62 x 10-7
Please note: The values of (stress) and t (exposure time) is read from a file. But the computation required to get the exact value of stress and time from the read file is
current value for shear stress=(current row+previous row)/2------------------(2)
current value for exposure time=current time-previous time=∆t---------(-3)
Put the calculated values for each current value of time and stress to calculate the D values based on equation 1
please how do i go about implementing this in matlab. i have imported the attached diument in MATLAB, seperating this variables and implementing the above equations for 2 and 3 for this sets of values is a challenge. can any one guide me.
_____________________________
Victor Albert on 21 Aug 2020 at 15:45
dear rik
i have imported the data.
how do i get the values for equation 2 and 3 from the long list of data(file.txt) and claulcte it for each D values
i know for and while loop statements can do that, but am not sure how to implement it in matlab. i had to export the data back to excel for computation.
but i want to be bale to implement this with matlab.
_____________________________
Victor Albert on 21 Aug 2020 at 15:46
but i want to be able to implement this with matlab, considering the long list of data sets( rows and column)
_____________________________
Victor Albert on 21 Aug 2020 at 18:41
fidi = fopen('file.xy','rt'); % to open the file and save it to a variable
% named fidi
st = fseek(fidi, 1, 'bof'); % Position File After First Line
k1 = 1; % Counter
while (st == 0) && (~feof(fidi)) % Test For End Of File Or Unsuccessful ‘fseek’ File Position
particle{k1} = textscan(fidi, '%f%f', 'Delimiter','\t', 'HeaderLines',4, 'CollectOutput',1);
st = fseek(fidi, 50, 'cof'); % Position File Pointer To Next Line After Stop
k1 = k1 + 1;
end
% function creation
function P = damage(time,stress)
a = 2.416;
b = 0.785;
f = 3.62*10^-7;
P = f* (stress^a) + (time^b);
end
_____________________________
Victor Albert on 21 Aug 2020 at 20:03
thats what i have written
_____________________________
Victor Albert on 22 Aug 2020 at [about 8:30]
file.txt is what i meant
_____________________________
Victor Albert on 22 Aug 2020 at [about 11:30]
the first column contains the time values and the second the stress values. this data were imported from a simulation done in Ansys fluent. there were 60 particles with each having certain number of stress and time values. i hope it clarifies.
particle 1
time stress
0 1.98974
2.51387e-05 2.00879
5.02775e-05 2.04566
7.50753e-05 2.06206
9.98731e-05 2.05735
0.000124671 2.05541
_____________________________
Victor Albert on 22 Aug 2020 at [about 12:30] [scalarvstime1.csv was attached to this comment]
i hope the attached help you. attached documents were only sent to excel to remove the strings.
Rena Berman on 12 Oct 2020 at 17:07

Rik on 22 Aug 2020
Well, if that is the input, then this edit to your function should work:
function P = damage(time,stress)
a = 2.416;
b = 0.785;
f = 3.62*10^-7;
P = f* (stress.^a) + (time.^b);
% ^ ^
% use element-wise power, instead of matrix multiplication
end

Victor Albert on 27 Aug 2020
time=diff(A(:,1));
P = damage(time,stress);
function P = damage(time,stress)
a = 2.416;
b = 0.785;
f = 3.62*10^-7;
P = f* (stress.^a) + (time.^b);
% ^ ^
% use element-wise power, instead of matrix multiplication
end
>> P = damage(time,stress)
Error: File: damage.m Line: 5 Column: 14
Function 'damage' has already been declared within this scope.
>> P = damage(time,stress)
Error: File: damage.m Line: 5 Column: 14
Function 'damage' has already been declared within this scope.
>> P = f* (stress.^a) + (time.^b)
Unrecognized function or variable 'f'.
Victor Albert on 27 Aug 2020
i tried to run the above code. this is the error i am still getting. what could go wrong here.
Rik on 27 Aug 2020
You can't have a function inside a script file with the same name.