How to integrate a data set?

I have a data set that includes data for temperature and specific heat capacity, and I need to integrate (Cv(T)/T)dT and (Cv(T))dT over 0 to 270. I uploaded the data into Matlab already, and I have a plot of the data (temp on x-axis, Cv on y-axis). How do I code for the cumulative entropy (see first equation) and energy (see second equation)?

 Accepted Answer

I would use the trapz function, or cumtrapz (linked to in the trapz documentation), depending on the result you want.
For example:
IntEq1 = trapz(T, Cv./T);
IntEq2 = trapz(T,Cv);
Without your data I am only guessing here, however these should at least be close to what you want. If you want vectors of interim results as well, replace trapz with cumtrapz in these assignments.

12 Comments

My data is a .csv file. Right now I have the data uploaded and in matrix form with
T = readtable(silver_data.csv);
C = T{:,:};
plot(C(:,1),C(:,2)).
That just gives me a plot of the data. Can I just directly add intEq1 after that?
I would do sometrhing like this:
SD = readtable('silver_data.csv', 'PreserveVariableNames',1);
T = SD{:,1};
Cv = SD{:,2};
IntEq1 = trapz(T, Cv./T)
IntEq2 = trapz(T,Cv)
figure
plot(T, Cv)
grid
producing:
IntEq1 =
9.70376967628969
IntEq2 =
1205.67613588139
Thank you!! I’ll try that. My teacher mentioned using Inte=cumtrapz though? Because I’ll need multiple cumulative entropies and energies (one for each time entry)?
My pleasure.
As I mentioned in my original Answer:
‘If you want vectors of interim results as well, replace trapz with cumtrapz in these assignments.
The code then becomes:
SD = readtable('silver_data.csv', 'PreserveVariableNames',1);
T = SD{:,1};
Cv = SD{:,2};
vars = SD.Properties.VariableNames;
IntEq1 = cumtrapz(T, Cv./T);
IntEq2 = cumtrapz(T,Cv);
figure
yyaxis left
plot(T, Cv)
grid
xlabel(vars{1})
ylabel(vars{2})
yyaxis right
hyr1 = semilogy(T, IntEq1);
hold on
hyr2 = plot(T, IntEq2);
hold off
grid
ylabel('Values of Integrals')
legend([hyr1 hyr2],'IntEq1', 'IntEq2', 'Location','SE')
This calculates them and plots them. I use the semilogy scale on the right axis so that both curves are easily visible.
Got it, thank you! Will this show me a table with the values as well or just the graph?
My pleasure.
That code will show you the plot.
This assignment:
SD = [SD table(IntEq1, IntEq2)];
will add them to the ‘SD’ table, so:
FirstFiveRows = SD(1:5,:)
produces:
FirstFiveRows =
Temp(K) C_v cal/deg/mole IntEq1 IntEq2
_______ ________________ ________ _______
9.8181 0.042043 0 0
11.786 0.078029 0.010728 0.11815
13.534 0.11397 0.023877 0.286
14.84 0.16766 0.036748 0.46983
16.367 0.21248 0.055285 0.76005
In the code, put that just after you calculate the integrals:
IntEq1 = cumtrapz(T, Cv./T);
IntEq2 = cumtrapz(T,Cv);
SD = [SD table(IntEq1, IntEq2)];
Thank you again, you're a lifesaver. Should I put the code for the table values in after the code for the plot?
My pleasure.
So long as you put the plots and the table concatenation code after the integration assignments, you can put them anywhere in your code you want. That is entirely your decision.
If my Answer helped you solve your problem, please Accept it!
Thank you so so so much. Sorry to ask this, but I'm completely new to coding, so would you mind explaining what some of your steps mean? Like what does SD mean? The hyr's? vars1?
Sure!
Your file is called ‘silver_data.csv’ so I called the table it created ‘SD’. (I might have called it ‘T’ for ‘table’, however you have a variable named ‘T’ for temperature.)
The ‘hyr’ variables are handles to the particular yyaxis right plots. They guarantee that the legend only refers to the curves in yyaxis right, and do not include the yyaxis left variables. I could have instead created it as:
legend('Data', 'IntEq1', 'IntEq2', 'Location','SE')
to include all of them.
The ‘vars’ variable contains the column headers of the ‘SD’ table, imported from the ‘silver_data.csv’ file. I used it to create the ylabel for yyaxis left and the common xlabel.
Got it! Thanks again for all your help. I really appreciate it.
As always, my pleasure!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!