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

Analisa Program Program No.1

This document contains two programs related to solving partial differential equations numerically using finite difference methods. Program 1 solves the 2D heat equation using an explicit finite difference scheme. It defines functions for inputting the matrix, calculating values based on neighboring points, and outputs the solution at each time step. Program 2 solves the wave equation in 1D. It similarly defines functions for inputting the matrix and calculating values at each time step based on the previous values and neighboring points. It includes options to save the output matrix to a text file. Both programs demonstrate common finite difference techniques for numerically solving PDEs on a grid by explicitly calculating future values from present neighboring point values. Program 1 focuses on the heat equation and

Uploaded by

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

Analisa Program Program No.1

This document contains two programs related to solving partial differential equations numerically using finite difference methods. Program 1 solves the 2D heat equation using an explicit finite difference scheme. It defines functions for inputting the matrix, calculating values based on neighboring points, and outputs the solution at each time step. Program 2 solves the wave equation in 1D. It similarly defines functions for inputting the matrix and calculating values at each time step based on the previous values and neighboring points. It includes options to save the output matrix to a text file. Both programs demonstrate common finite difference techniques for numerically solving PDEs on a grid by explicitly calculating future values from present neighboring point values. Program 1 focuses on the heat equation and

Uploaded by

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

Analisa Program

Program no.1
/* heat equation 2D */
#include <conio.h>
#include <stdio.h>
#include <math.h>
float Gamma;
void input_matrixfloat !"#$$%"#$$%& int p'
(
int )& l;
for)*$; )<p*p; )++'
(
forl*$; l<p*p; l++'
(
ifl**)'
!")%"l% * #,-*Gamma';
else ifl**),p .. l**)+p'
!")%"l% * Gamma;
else ifl**)+# // l0p1*$'
!")%"l% * Gamma;
else ifl**),# // )0p1*$'
!")%"l% * Gamma;
else
!")%"l% * $;
2
2
2
float ffloat d& float 3'
(
return 4*exp,4$*,$.#'*d,$.#'',3,$.24'*3,$.24''''';
2
float hfloat o& float p'
(
return o*o'+p*p''/4$';
2
main'
(
int i& 5& )& f& n& x& 6;
float m& a& Gamma& Dx& Dt& alpha& 7& xi& 65& 8"#$$%"#$$%& !"#$$%
"#$$%;
void input_matrixfloat !"#$$%"#$$%& int p';

printf9:n:t:t ;eat <quation 2D:n 9';
printf 9 :n:n:n =ilai x dan nilai 6 ma)simal * 9'; scanf90f9& /a';
printf 9 :n =ilai 7 ma)simal * 9'; scanf90f9& /7';
printf 9 :n !an6a)n6a 3rid 6an3 diin3in)an * 9'; scanf90f9& /n';
printf 9 :n )onstanta alpha * 9'; scanf90f9& /alpha';
Dx * a/n+#';
Dt * Dx*Dx'/>*alpha';
//s6arat sta?ilitas para?oli) 2D
Gamma * alpha*Dt/Dx*Dx';

input_matrix8& n';
//men3input matrix 8

printf 9Dt * 0f:n9&Dt';
printf 9Dx * 0f:n9&Dx';
printf 9Gamma * 0f:n9& Gamma';

printf 9:n:n:n @87ABCD 8:n:n:n9';
for i*$; i<n*n; i++'
(
for 5*$; 5<n*n; 5++'
(
printf90.2f:t9& 8"i%"5%';
2
printf 9:n9';
2

m * 7/Dt;
printf 9:n m * 0d:n9& m';
printf 9:n ;asil * :n9';
printf 9t:ti xi':t:t5 65':t:t!x&6':n9';
for i*$; i<m; i++'
(
printf9:n0.2f9& i+#'*Dt';
(
for 5*$; 5<n*n; 5++'

!"i+#%"5% +* 8"5%")% * !"i%")%;

ifn 1* $ // 5+#'0n ** $'
x*n;

else
x * 5+#'0n;
xi * x*Dx;
6 * 5/n'+#;
65 * 6*Dx;
!"i+#%"5% +* xi&65'*Dt;

if6**#'
!"i+#%"5% +* Gamma*hxi&$';
if6**n'
!"i+#%"5% +* Gamma*hxi&65+Dx';
ifx**#'
!"i+#%"5% +* Gamma*h$&65';
ifx**n'
!"i+#%"5% +* Gamma*hxi+Dx&65';

printf9:t0d 0.2f':t0d 0.2f':t0f:n9& x& xi& 6& 65&
!"i+#%"5%';printf9:n9';
2
2
return $;
2
Program no 2.
/*Eersamaan Gelom?an3*/
#include <stdio.h>
#include <math.h>
#include <conio.h>
float 3amma;
void input_matrixfloat 8"2$$%"2$$%& int p'
(
int i& 5;
fori*$; i<p+#; i++'
(
for5*$; 5<p+#; 5++'
(
if5**i'
8"i%"5% * 2*#,3amma';
else if5**i,# .. 5**i+#'
8"i%"5% * 3amma;
else
8"i%"5% * $;
2
2
2


main'
(
//de)larasi varia?el//
FBG< *fp;
int u& v& 6& parsial;
float t& x& ?eta& dt& dx& !"2$$%"2$$%& @"2$$%"2$$%& n ;
printf9:n:t:t Ero3ram Eersamaan Gelom?an3:n 9';
printf9:n @asu)an nilai ma)simal untu) t H 9'; scanf90f9& /t';
printf9:n @asu)an nilai ma)simal untu) x H 9'; scanf90f9& /x';
printf9:n @asu)an 5umlah parsial 6an3 diin3in)an H 9'; scanf90d9&
/parsial';
printf9:n @asu)an nilai )onstanta ?etta 6an3 telah ditentu)an H 9';
scanf90f9& /?eta';
printf9:n9';
dx * x/parsial+#';
dt * sqrtdx*dx/-*?eta''; /* dtI2 <* dxI2/?etta merupa)an s6arat
sta?ilitas */
3amma * ?eta*dt*dt' /dx*dx';
input_matrix!& parsial';
printf9nilai dt H 0f:n9& dt';printf9:n9';
printf9nilai dx H 0f:n9& dx';printf9:n9';
printf9nilai 3amma H 0f:n9& 3amma';printf9:n9';
printf9:n@atri)s !:n9';printf9:n9';

foru*$; u<parsial+#; u++'
(
forv*$; v<parsial+#; v++'
(
printf90f:t9& !"u%"v%';
2
printf9:n9';
2
n*t/dt;
printf9:n@atri)s @"u%"6%:n9';
printf9iterasi:tt:t:t @"u%"6%:n9';
foru*$; u<parsial; u++'
(
printf9:n0d9& u+#';
forv*$; v<n+#; v++'
(
@"u+#%"v% * $;
for6*$; 6<parsial+#; 6++'
(
ifu**$'
@"u%"6%* sinJ.#-*6*dx/4K.J2'/2;
@"u+#%"u% +* !"v%"6%*@"u%"6%;
2
ifu>$'
@"u+#%"v% ,* @"u,#%"v%;
printf9:t0f:t0f:n9& u+#'*dt& @"u+#%"v%';
2
2
3etch ';
int f& i& 5& par_a& par_?& L; char namafile "J$$%;
savefileH
printf9:n:n:t8)an disimpan dalam FileM:n:t7e)an # * Nes & te)an 2 * =o
H 9';scanf90d9&/f';
iff**#'
(
sprintfnamafile&9trace.txt9';
fp * fopennamafile& 9a+9';
fprintffp&9:nB=EO7 @87ABCD !:n9';
for i*#;i<*par_a;i++'
(
for 5*#;5<*par_?;5++'
(
fprintffp&90d 9&!"u%"v%';
2
fprintffp&9:n9';
2
fprintffp&9:n;8DBG 7A8L<:n9';
fprintffp&90d 9&L';
fclosefp';
printf9:n:n:t:tFile 7elah Disimpan den3an =ama Ptrace.txtP:n9';
2
else iff**2'
(
printf9:n:tData 7ida) Disimpan dalam File:n9';
2
else
(
printf9:n:tCode 6an3a Diinput)an salah9';
3oto savefile;
2
return $;
2
Program no.1
/*distri?usi panas pelat )ondu)tor*/
#include <conio.h>
#include <math.h>
#include <stdio.h>
main'
(
int i&5;
float x& 6& Dx& D6& Dx#& Dx2& D6#& D62& )& Q& m& n& c& !"#$$%"#$$%;
FBG< *fp;
char namafile"4$%;
printf9:tdistri?usi panas pelat )ondu)tor c:n:n9';
printf9:n:npan5an3 x * 9';scanf90f9&/x';
printf9:npan5an3 6 * 9';scanf90f9&/6';
printf9:nnilai delta x * 9';scanf90f9&/Dx';
printf9:nnilai delta 6 * 9';scanf90f9&/D6';
printf9:nnilai 3enerasi )alor Q' * 9';scanf90f9&/Q';
printf9:nnilai )ondu)tivitas termal)' * 9';scanf90f9&/)';
printf9:n:n9';

m*x/Dx;
n*6/D6;
c*,Q/);

FBG< * data;
data * fopen9heatequation#.xls9&9R9';

fori*#;i<n;i++'
(
for5*#;5<m;5++'
( if i**#'
!"i%"5%*!"i+#%"5%+!"i,#%"5%+!"i%"5+#%,c*Dx*Dx*D6*D6'
+J$*D6''/J;
else if i**n'
!"i%"5%*!"i+#%"5%+!"i,#%"5%+!"i%"5,#%,
c*Dx*Dx*D6*D6',#$*D6''/J;
else
!"i%"5%*!"i+#%"5%+!"i,#%"5%+!"i%"5+#%+!"i%"5,#%,
c*Dx*Dx*D6*D6''/-;
2
2

fori*$;i<*n;i++'
(
for5*#;5<m;5++'
(
printf9 0f9&!"i%"5%';
fprintfdata& 9:n 0.Jf9&!"i%"5%';
2
printf9:n:n9';
fprintfdata& 9:n9';
2
fclosedata';

3etch';
2
7ampilan pro3ram

You might also like