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.

8 Comments

Which of the steps are causing you problems?
This looks like homework. You can find guidelines for posting homework on this forum here.
Show your code. I haven't hacked your computer, so you will have to post it here.
Your code doesn't look like it will work with the attached data. The file names don't match either.
Comment posted as answer by Victor Albert:
sorry, it was file.xy
just a typo error
Your code results in a cell vector containing cell scalars each with an n by 2 double array. There are only 2 cells were the second column has non-zero values: 43 (6/1141) and 48 (723/1288).
How is time stored in that data, and how is stress stored in that data? I don't understand what your data structure means.
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.
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
% reading the file
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.
(Answers Dev) Restored edit

Sign in to comment.

Answers (1)

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

13 Comments

you havent answered it yet. Equation 2 and 3 computation are taken from the the spreadsheet ( read the file). that is the reason i was asking the question. from the spreadsheet you compute the equation 2 and 3 and put it back to equation 1.
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
For delta t you can use the diff function, for the mean of two elements you can use movmean.
can you express it here for me to understand it and implment it. this does not help me.
Since you were able to write quite complex code to read your data, I assumed you would be fine with writing the code yourself with these two function, but here you go:
A=readmatrix('scalarvstime1.csv');
time=diff(A(:,1));
stress=movmean(A(:,2),[1 0],'Endpoints','discard');
P = damage(time,stress);
Not that this doesn't split the calculations for the different time ranges, so some delta t values are negative. That results in complex values. If you want to prevent this you should split your data.
what if an absolute function is used for the negative values?. will that solve the problem of negativeity.
i was thinking a loop fuction could easy help out here, what do you think.
can you please throw mor light on the below for me:a kind of confusing!
stress=movmean(A(:,2),[1 0],'Endpoints','discard');
abs would make sure all your times are positive, but that is not your goal. You should split the calcutation instead.
You could use a loop, but what would you want to do with it? The diff and movmean function are already doing the calculation, and those are likely more efficient than anything you would be able to write.
Have you read the documentation for the movmean function? It explains this syntax. What part didn't you understand?
Well, if you can convert the output of the code you posted to pairs of vectors, you can use the code I posted. Otherwise I'm not sure what you mean.
rik,
i didnt know what happen, i was supposed to vote for this. sorry about that
thanks for restoring this thread.
How can this happen by accident? You get a confirmation box before you can delete your comment. And how can you accidentally edit away most of your question?
If you wanted to vote for my answer, you haven't done so yet (nor have you accepted it).
A=readmatrix('scalarvstime1.csv');
time=diff(A(:,1));
stress=movmean(A(:,2),[1 0],'Endpoints','discard');
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'.
i tried to run the above code. this is the error i am still getting. what could go wrong here.
You can't have a function inside a script file with the same name.

Sign in to comment.

Asked:

on 21 Aug 2020

Commented:

on 12 Oct 2020

Community Treasure Hunt

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

Start Hunting!