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)?
採用された回答
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?
Star Strider
2020 年 2 月 16 日
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)?
Star Strider
2020 年 2 月 16 日
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?
Star Strider
2020 年 2 月 16 日
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?
Star Strider
2020 年 2 月 16 日
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!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
