How to integrate a data set?
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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)?
채택된 답변
Star Strider
2020년 2월 16일
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
Chuqi Zheng
2020년 2월 16일
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
Chuqi Zheng
2020년 2월 16일
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.
Chuqi Zheng
2020년 2월 16일
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)];
Chuqi Zheng
2020년 2월 16일
Thank you again, you're a lifesaver. Should I put the code for the table values in after the code for the plot?
Star Strider
2020년 2월 16일
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!
Chuqi Zheng
2020년 2월 16일
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.
Chuqi Zheng
2020년 2월 16일
Got it! Thanks again for all your help. I really appreciate it.
Star Strider
2020년 2월 16일
As always, my pleasure!
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Numerical Integration and Differentiation에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
