How to resolve the algorithm Resistor mesh step by step in the C programming language

Published on 7 June 2024 03:52 AM
#C

How to resolve the algorithm Resistor mesh step by step in the C programming language

Table of Contents

Problem Statement

Given   10×10   grid nodes   (as shown in the image)   interconnected by   1Ω   resistors as shown, find the resistance between points   A   and   B.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Resistor mesh step by step in the C programming language

This C program simulates a 2D heat equation solver using the finite difference method to approximate the solution. It models a square grid of points with varying temperatures and calculates the heat flow between them. Here's a breakdown:

  • Structure and Constants: The node structure represents grid points with their temperature (v) and a flag for fixed points (fixed). S defines the grid size.

  • Memory Allocation: alloc2() allocates memory for a 2D array of nodes.

  • Boundary Conditions: set_boundary() sets the values of two specific points (1,1) and (6,7) on the grid.

  • Calculating the Difference: calc_diff() calculates the difference between the current temperature of each point and the average temperature of its neighbors. It ignores fixed points. The total difference is returned.

  • Iteration: iter() iteratively updates the grid using the finite difference method until the difference becomes minimal. It returns the difference between the average temperatures of non-fixed and fixed points.

  • Main Function: main() creates a mesh grid, runs the iter() simulation, and prints the calculated thermal resistance (R) as 2 divided by the difference returned by iter().

Source code in the c programming language

#include <stdio.h>
#include <stdlib.h>
 
#define S 10
typedef struct { double v; int fixed; } node;
 
#define each(i, x) for(i = 0; i < x; i++)
node **alloc2(int w, int h)
{
	int i;
	node **a = calloc(1, sizeof(node*)*h + sizeof(node)*w*h);
	each(i, h) a[i] = i ? a[i-1] + w : (node*)(a + h);
	return a;
}
 
void set_boundary(node **m)
{
	m[1][1].fixed =  1; m[1][1].v =  1;
	m[6][7].fixed = -1; m[6][7].v = -1;
}
 
double calc_diff(node **m, node **d, int w, int h)
{
	int i, j, n;
	double v, total = 0;
	each(i, h) each(j, w) {
		v = 0; n = 0;
		if (i) v += m[i-1][j].v, n++;
		if (j) v += m[i][j-1].v, n++;
		if (i+1 < h) v += m[i+1][j].v, n++;
		if (j+1 < w) v += m[i][j+1].v, n++;
 
		d[i][j].v = v = m[i][j].v - v / n;
		if (!m[i][j].fixed) total += v * v;
	}
	return total;
}
 
double iter(node **m, int w, int h)
{
	node **d = alloc2(w, h);
	int i, j;
	double diff = 1e10;
	double cur[] = {0, 0, 0};

	while (diff > 1e-24) {
		set_boundary(m);
		diff = calc_diff(m, d, w, h);
		each(i,h) each(j, w) m[i][j].v -= d[i][j].v;
	}
 
	each(i, h) each(j, w)
		cur[ m[i][j].fixed + 1 ] += d[i][j].v *
				(!!i + !!j + (i < h-1) + (j < w -1));
 
	free(d);
	return (cur[2] - cur[0])/2;
}
 
int main()
{
	node **mesh = alloc2(S, S);
	printf("R = %g\n", 2 / iter(mesh, S, S));
	return 0;
}


  

You may also check:How to resolve the algorithm Apply a callback to an array step by step in the Salmon programming language
You may also check:How to resolve the algorithm 100 prisoners step by step in the Raku programming language
You may also check:How to resolve the algorithm Input loop step by step in the NodeJS programming language
You may also check:How to resolve the algorithm Unbias a random generator step by step in the NetRexx programming language
You may also check:How to resolve the algorithm Arrays step by step in the AmigaE programming language