0% found this document useful (0 votes)
8 views

Assigment 4

Uploaded by

pranaybolli2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Assigment 4

Uploaded by

pranaybolli2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <stdio.

h>
#include <stdlib.h>
#include <math.h>

#define MAX_DATA_POINTS 1000


#define PI 3.141592653589793

typedef struct {
double time; // Time
double voltage; // Voltage
double current; // Current
} Measurement;

void read_data(const char *filename, Measurement measurements[], int


*num_measurements) {
FILE *file = fopen(filename, "r");
if (!file) {
perror("Failed to open file");
exit(EXIT_FAILURE);
}

*num_measurements = 0;
while (fscanf(file, "%lf %lf %lf", &measurements[*num_measurements].time,
&measurements[*num_measurements].voltage,
&measurements[*num_measurements].current) == 3) {
(*num_measurements)++;
}

fclose(file);
}

void write_results(const char *filename, double *data1, double *data2, int count,
int increment) {
FILE *file = fopen(filename, "w");
if (!file) {
perror("Failed to open file for writing");
exit(EXIT_FAILURE);
}

for (int j = 0; j < count; j += increment) {


fprintf(file, "%f\t%f\n", data1[j], data2[j]);
}

fclose(file);
}

void generate_plot_script(const char *output_filename) {


FILE *file = fopen("plot_script.gp", "w");
if (!file) {
perror("Failed to open plot script file");
exit(EXIT_FAILURE);
}

fprintf(file, "set terminal jpeg\n");


fprintf(file, "set output '%s'\n", output_filename);
fprintf(file, "set xlabel 'Time (seconds)'\n");
fprintf(file, "set ylabel 'Power per Unit Mass (W/kg)'\n");
fprintf(file, "set title 'Time vs Power per Unit Mass'\n");
fprintf(file, "plot 'Joule_Heat_in_3YSZ.txt' using 1:2 title 'Power in 3YSZ'
with lines\n");

fclose(file);
}

int main() {
Measurement measurements[MAX_DATA_POINTS];
int num_measurements;

// Load data
read_data("PZT.txt", measurements, &num_measurements);

// Constants
double density_PZT = 8000; // kg/m^3
double dia_PZT = 6e-3; // m
double h_PZT = 5.5e-3; // m
double area_PZT = PI * (dia_PZT / 2.0) * (dia_PZT / 2.0); // m^2

double R_inconel = 1.31e-6; // ohm-m


double density_inconel = 8113; // kg/m^3
double dia_inconel = 0.0182; // m
double h_inconel = 3e-3; // m

// Initialize arrays for calculations


double power[MAX_DATA_POINTS], power_m[MAX_DATA_POINTS];
double joule_inconel_m[MAX_DATA_POINTS];

// Calculations
for (int i = 0; i < num_measurements; i++) {
power[i] = measurements[i].voltage * measurements[i].current; // Power in W

double vs = area_PZT * h_PZT; // Volume of the sample in m^3


power_m[i] = power[i] / (vs * density_PZT); // Power per unit mass in W/kg

joule_inconel_m[i] = (measurements[i].current * measurements[i].current *


R_inconel * h_inconel) /
(PI * (dia_inconel / 2.0) * (dia_inconel / 2.0) *
density_inconel); // Joule heating per unit mass in W/kg
}

// Write results to files


write_results("Joule_Heat_in_3YSZ.txt", measurements[0].time, power_m,
num_measurements, 4);
write_results("Joule_Heat_in_Inconel.txt", measurements[0].time,
joule_inconel_m, num_measurements, 4);

// Generate Gnuplot script


generate_plot_script("Power_vs_Time.jpg");

// Call Gnuplot to create the graph


system("gnuplot plot_script.gp");

return 0;
}

You might also like