Message Boards Message Boards

[✓] Use DynamicModule for better presentation?

GROUPS:

Hi

I have the program for solving equations by FDM & FEM , so i need the program in DynamicModule that show both FDM & FEM results to see the difference between them , and i want to present them by DynamicModule for better & professional presenting , can U help me to find my errors on program that i write in DynamicModule?

Thank You

"FDM Code"

ClearAll["Global`*"];
n = Input["n"];
m = n - 1; \[Alpha] = -4 + h^2; \[Beta] = 6 - 2 h^2 + h^4; h = 1/n;
Do[Do[a[i, j] = 0, {i, 1, m}], {j, 1, m}];
Do[b[j, 1] = 0, {j, 1, m}]; b[m, 1] = -9 - 3 \[Alpha] - 5 h^3; 
b[1, 1] = 3 + 2 h - h^2; b[2, 1] = -1; 
b[m - 1, 1] = -3; If[n == 4, b[2, 1] = -4, b[2, 1] = -1];
Do[a[j, j] = \[Beta], {j, 1, m - 1}];
Do[a[j, j] = \[Beta] - 3, {j, m, m}];
Do[a[j, j + 1] = \[Alpha], {j, 1, m - 1}];
Do[a[j + 1, j] = \[Alpha], {j, 1, m - 1}];
Do[a[j + 1, j] = \[Alpha] + 1, {j, m - 1, m}];
Do[a[j, j + 2] = 1., {j, 1, m - 2}];
Do[a[j + 2, j] = 1., {j, 1, m - 2}];
A = Array[a, {m, m}];
B = Array[b, {m, 1}];
Y = Flatten[LinearSolve[A, B]];
ListPlot[{Y}, Joined -> True, PlotMarkers -> Automatic]

"FEM Code"

DSolve[{u''''[x] + u''[x] + u[x] == 0, u[0] == 1, u'[0] == 2, 
   u[1] == 3, u'''[1] == 5}, u[x], x];
(*Plot[u[x]/.%,{x,1,2}]*)
u[x_] = u[x] /. %;
{u[0.25], u[0.5], u[0.75]}

"DynamicModule Code "

DynamicModule[
 m = n - 1; \[Alpha] = -4 + h^2; \[Beta] = 6 - 2 h^2 + h^4; h = 1/n;
 Do[Do[a[i, j] = 0, {i, 1, m}], {j, 1, m}];
 Do[b[j, 1] = 0, {j, 1, m}]; b[m, 1] = -9 - 3 \[Alpha] - 5 h^3; 
 b[1, 1] = 3 + 2 h - h^2; b[2, 1] = -1; b[m - 1, 1] = -3; 
 If[n == 4, b[2, 1] = -4, b[2, 1] = -1];
 Do[a[j, j] = \[Beta], {j, 1, m - 1}];
 Do[a[j, j] = \[Beta] - 3, {j, m, m}];
 Do[a[j, j + 1] = \[Alpha], {j, 1, m - 1}];
 Do[a[j + 1, j] = \[Alpha], {j, 1, m - 1}];
 Do[a[j + 1, j] = \[Alpha] + 1, {j, m - 1, m}];
 Do[a[j, j + 2] = 1., {j, 1, m - 2}];
 Do[a[j + 2, j] = 1., {j, 1, m - 2}];
 A = Array[a, {m, m}];
 B = Array[b, {m, 1}];
 Y = Flatten[LinearSolve[A, B]]; {z = 0.00001}, {Panel[
   SetterBar[Dynamic[n], Range[4, 20]], 
   Dynamic[ListPlot[{Y}, Joined -> True, PlotMarkers -> Automatic]]]}]
POSTED BY: Saleh Baradaran
Answer
4 months ago

DynamicModule assumes you give a list of local variables:

DynamicModule[{fdm, n, m, \[Alpha], \[Beta], h, a, b, A, B},
 fdm[n_] := (m = n - 1; \[Alpha] = -4 + h^2; \[Beta] = 
    6 - 2 h^2 + h^4; h = 1/n; 
   Do[Do[a[i, j] = 0, {i, 1, m}], {j, 1, m}];
   Do[b[j, 1] = 0, {j, 1, m}];
   b[m, 1] = -9 - 3 \[Alpha] - 5 h^3;
   b[1, 1] = 3 + 2 h - h^2; b[2, 1] = -1;
   b[m - 1, 1] = -3;
   b[2, 1] = If[n == 4, -4, -1];
   Do[a[j, j] = \[Beta], {j, 1, m - 1}];
   Do[a[j, j] = \[Beta] - 3, {j, m, m}];
   Do[a[j, j + 1] = \[Alpha], {j, 1, m - 1}];
   Do[a[j + 1, j] = \[Alpha], {j, 1, m - 1}];
   Do[a[j + 1, j] = \[Alpha] + 1, {j, m - 1, m}];
   Do[a[j, j + 2] = 1., {j, 1, m - 2}];
   Do[a[j + 2, j] = 1., {j, 1, m - 2}];
   A = Array[a, {m, m}];
   B = Array[b, {m, 1}];
   Flatten[LinearSolve[A, B]]);
 Panel[SetterBar[Dynamic[n], Range[4, 20]], 
  Dynamic[ListPlot[{fdm[n]}, Joined -> True, 
    PlotMarkers -> Automatic]]]]

You may consider using SparseArray to define your matrix A:

SparseArray[{
  ({i_, i_} /; i < m) -> \[Beta],
  {m, m} -> \[Beta] - 3,
  ({i_, j_} /; Abs[i - j] == 1 && i < m) -> \[Alpha],
  {m, m - 1} -> \[Alpha] + 1,
  ({i_, j_} /; Abs[i - j] == 2) -> 1.}, {m, m}]
POSTED BY: Gianluca Gorni
Answer
4 months ago

Thank U Gianluca

I want to know more about how to put FEM sloution in this code to see the difference about FEM & FDM on the result of Diagram....

Best Regards

POSTED BY: Saleh Baradaran
Answer
4 months ago

This may be a start:

DynamicModule[{fdm, n, m, \[Alpha], \[Beta], h, a, b, A, B},
 fdm[n_] := (m = n - 1; \[Alpha] = -4 + h^2; \[Beta] = 
    6 - 2 h^2 + h^4; h = 1/n;
   Do[Do[a[i, j] = 0, {i, 1, m}], {j, 1, m}];
   Do[b[j, 1] = 0, {j, 1, m}];
   b[m, 1] = -9 - 3 \[Alpha] - 5 h^3;
   b[1, 1] = 3 + 2 h - h^2; b[2, 1] = -1;
   b[m - 1, 1] = -3;
   b[2, 1] = If[n == 4, -4, -1];
   Do[a[j, j] = \[Beta], {j, 1, m - 1}];
   Do[a[j, j] = \[Beta] - 3, {j, m, m}];
   Do[a[j, j + 1] = \[Alpha], {j, 1, m - 1}];
   Do[a[j + 1, j] = \[Alpha], {j, 1, m - 1}];
   Do[a[j + 1, j] = \[Alpha] + 1, {j, m - 1, m}];
   Do[a[j, j + 2] = 1., {j, 1, m - 2}];
   Do[a[j + 2, j] = 1., {j, 1, m - 2}];
   A = Array[a, {m, m}];
   B = Array[b, {m, 1}];
   Flatten[LinearSolve[A, B]]); 
 u = DSolveValue[{u''''[x] + u''[x] + u[x] == 0, u[0] == 1, 
    u'[0] == 2, u[1] == 3, u'''[1] == 5}, u, x];
 Panel[SetterBar[Dynamic[n], Range[4, 20]], 
  Dynamic[{ListPlot[fdm[n], Joined -> True, 
     PlotMarkers -> Automatic],
    Plot[u[x], {x, 0, 1}]}]]]

You can adjust the details of the plots to compare them better. I don't know what you have in mind exactly.

POSTED BY: Gianluca Gorni
Answer
4 months ago

Group Abstract Group Abstract