Help to convert C++ to Matlab

#include <stdio.h>
int main() {
// Specific heat capacity of liquid water: Cp = 4.18 J/g/°C
double Cp = 4.18;
// Mass of water in grams
double m = 1000;
// Initial temperature in °C
double T1 = 0;
// Final temperature in °C
double T2 = 100;
// Temperature increment in °C
double dT = 5;
printf("Temperature (°C) | Heat Required (J)\n");
printf("-----------------|--------------------\n");
for (double T = T1; T <= T2; T += dT) {
double Q = m * Cp * (T - T1);
printf("%17.2lf | %20.2lf\n", T, Q);
}
return 0;
}

Answers (1)

Matt J
Matt J on 26 Dec 2022
Edited: Matt J on 26 Dec 2022
function main
% Specific heat capacity of liquid water: Cp = 4.18 J/g/°C
Cp = 4.18;
% Mass of water in grams
m = 1000;
% Initial temperature in °C
T1 = 0;
% Final temperature in °C
T2 = 100;
% Temperature increment in °C
dT = 5;
fprintf("Temperature (°C) | Heat Required (J)\n");
fprintf("-----------------|--------------------\n");
for T=(T1:dT:T2)
Q = m * Cp * (T - T1);
fprintf("%17.2f|%20.2f\n", T, Q);
end
end

Categories

Find more on General Applications in Help Center and File Exchange

Tags

Asked:

on 26 Dec 2022

Edited:

on 26 Dec 2022

Community Treasure Hunt

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

Start Hunting!