Hello, Could someone help with this. I am having trouble running a finite difference problem in c++. Here is the problem and my code is right below. Thanks in advance for every help.
Consider the cooling of a sphere of radius R, initially at a uniform temperature T0, and
subjected to a uniform convective heat flux at its surface into a
medium at T∞ with a heat transfer coefficient h.
(a) Solve by finite differences to calculate the variation of
temperature within the sphere as a function of time and radius.
Draw a series of temperature profiles for a stainless steel
sphere under the following conditions (assumed constant):
ρ = 7820 ; C = 460.8 ; k = 20; T = 350
T(infinity) = 50; h=100 ; R = 0.02
(B) Examine the influence of the mesh sizes Δr and the
time step Δt to make sure the solution obtained is
independent of the step size and the time step that
were selected.
(c) Draw few temperature profiles for some
intermediate time for three cases: (1) k = 20 W/m s, (2)
k = 2 W/m s, and (3) k = 0.2 W/m s. All the other
variables remain constant. Briefly discuss your
results.
The 3 temperature profile equations needed are (where i is distance from the center of the sphere (code for radius) and n is the time)::
if (i==1 && (n!=0 || n!=4000) ) {
T[i][n] = (1+6*y)*T[i][n+10] - 6*y*T[i+1][n+10]; // equation when i=1
}
else if (i==N) {
T[i][n] = T[i][n+10]*(1+2*y-2*y*dr*h/k*(1+1/(i-1)))-2*y*T[i-1][n+10]+2*y*dr*h/k*(1+1/(i-1))*Tn; //equation when i=Nr where Nr is the surface of the sphere and here the radius is the radius of the sphere, r=0.02.
}
else {
T[i][n] = -y*(1-1/(2*(i-1)))*T[i-1][n+10]-y*(1+1/(2*(i-1)))*T[i+1][n+10]+(1+2*y)*T[i][n+10];// equation for all other ith points
}
My code::
using namespace std;
#include<iostream>
#include <math.h>
#include <stdio.h>
int main() {
double T[11][5000],y, a, k, r,cp,rho,dr,dt,t,Tn,h,b,c,a1;
int i, n, N;
for (i=1; i<=4; i++) {
//&& (T[i][n]>50 && T[i][n]<=350)
c=1;
while (c>=0.1){
if (n==0){
T[i][n]=350; // boundary condition. initial temperature of the sphere is the same through out
}
else if (n==4000)
{
T[i][n]=50; // final temperature of the final at every ith position
}
else if (i==1 && (n!=0 || n!=4000) ) {
T[i][n] = (1+6*y)*T[i][n+10] - 6*y*T[i+1][n+10]; // equation when i=1
}
else if (i==N && (n!=0 ||n!=4400) ) {
T[i][n] = T[i][n+10]*(1+2*y-2*y*dr*h/k*(1+1/(i-1)))-2*y*T[i-1][n+10]+2*y*dr*h/k*(1+1/(i-1))*Tn; //equation when i=Nr where Nr is the surface of the sphere and here the radius is the radius of the sphere
}
else {
T[i][n] = -y*(1-1/(2*(i-1)))*T[i-1][n+10]-y*(1+1/(2*(i-1)))*T[i+1][n+10]+(1+2*y)*T[i][n+10];// equation for all other ith points
}
//r=r+0.002;
}
c = T[i][n]-a1;// equation to converge T values of the equations
a1=T[i][n];
cout<<T[i][n]<<i<<n<<endl; //output Temperature values at all i points for every n time
1) Your code is not wrapped in [code] blocks. And it's not formatted so it's very difficult to read.
2) You haven't actually asked a question. "I have a problem" is a statement.