Skip to main content
added 170 characters in body
Source Link
user516076
  • 373
  • 1
  • 8

I have a mathematica code for solving numerical inverse Laplace transform (Credit to Mr. Patrick O. Kano), but sadly the code is using Nintegrate, and i want to use Simpson's rule with adjustable stepsize, and function. By the way my numerical ILT is based on Talbot's method. For more details about Talbot's method, parametrization and its contour, i've mentioned it on my previous question here: Need help to plot Talbot's Contour (Newbie question)

Hope this doesn't sound complicated. So here is my Mathematica code for numerical ILT.

Timeval = 2;
Rval = 2;
Flap[s_] = 1/(s - 1);
Tfunexact[t_] = Exp[1*t];
Valexact = N[Tfunexact[Timeval], 10]

STalbot[r_, a_] = r*a*Cot[a] + I*r*a;
dsda[r_, a_] = (1 + I*(a + Cot[a]*(a*Cot[a] - 1)));
TimeDfun[r_, t_] = 
  Rval/(Pi)*
   NIntegrate[
    Exp[STalbot[r, a]*t]*Flap[STalbot[r, a]]*dsda[r, a], {a, 0, Pi}, 
    WorkingPrecision -> 10];

{Timeval, Approxval} = Timing[TimeDfun[Rval, Timeval]]
RelError = Abs[Approxval - Valexact]/Valexact

Notice the Nintegrate there, i want to replace it with Simpson's rule, and here is my attempt [New Edit]:

Clear[a, bsimp[r_, n]
simpson[r_t_, 
  t_]M_] := (Rvalr/(3 nM)) (r*Exp[r*t]*Flap[r] 
 +  
  r*Exp[r t]*Flap[r] +2*Sum[Exp[
  
     2 Sum[Exp[STalbot[rt*STalbot[r, 2 i \[Theta]]*t]*Flap[STalbot[ri*Pi/M]*Flap[STalbot[r, \[Theta]]]*2 i*Pi/M]]*
        dsda[r, \[Theta]]] +2 i*Pi/M]], 
 {i, 1, M/2 - 41}] Sum[Exp[STalbot[r,+ (2 
 i - 1) \[Theta]]*t]*4*Sum[Exp[
       Flap[STalbot[rt*STalbot[r, (2 i - 1)*Pi/M]*Flap[STalbot[r, \[Theta]]]*(2 i - 1)*Pi/M]]*
        dsda[r, (2 i - 1)*Pi/M]], \[Theta]]]{i, 1, M/2 }])
    
f[s_]Flap[s_] := 1/(s - 1)
TableForm[;
 Table[TableForm[Table[{nM, N[simpson[0N[simp[0, \[Pi]Pi, n]]M]]}, {nM, 10, 100, 10}], 
  TableHeadings -> {{}, {"n""M", "s_n"}}]]]

{Timeval, Approxval} = 
 Timing[Re[simp[Rval, Timeval, 50, WorkingPrecision -> 10]]]
RelError = Abs[Approxval - Valexact]/Valexact

Can you please help me to accomplish this. I was failed to perform the lastNow, my code shows the result, but still the result was incorrect. Please comment if there's something unclear about my post. Thanks in advance!

I have a mathematica code for solving numerical inverse Laplace transform (Credit to Mr. Patrick O. Kano), but sadly the code is using Nintegrate, and i want to use Simpson's rule with adjustable stepsize, and function. By the way my numerical ILT is based on Talbot's method. For more details about Talbot's method, parametrization and its contour, i've mentioned it on my previous question here: Need help to plot Talbot's Contour (Newbie question)

Hope this doesn't sound complicated. So here is my Mathematica code for numerical ILT.

Timeval = 2;
Rval = 2;
Flap[s_] = 1/(s - 1);
Tfunexact[t_] = Exp[1*t];
Valexact = N[Tfunexact[Timeval], 10]

STalbot[r_, a_] = r*a*Cot[a] + I*r*a;
dsda[r_, a_] = (1 + I*(a + Cot[a]*(a*Cot[a] - 1)));
TimeDfun[r_, t_] = 
  Rval/(Pi)*
   NIntegrate[
    Exp[STalbot[r, a]*t]*Flap[STalbot[r, a]]*dsda[r, a], {a, 0, Pi}, 
    WorkingPrecision -> 10];

{Timeval, Approxval} = Timing[TimeDfun[Rval, Timeval]]
RelError = Abs[Approxval - Valexact]/Valexact

Notice the Nintegrate there, i want to replace it with Simpson's rule, and here is my attempt:

Clear[a, b, n]
simpson[r_, 
  t_] := (Rval/(3 n)) ( 
    r*Exp[r t]*Flap[r] +  
     2 Sum[Exp[STalbot[r, 2 i \[Theta]]*t]*Flap[STalbot[r, \[Theta]]]*
       dsda[r, \[Theta]]] +  
     4 Sum[Exp[STalbot[r, (2 i - 1) \[Theta]]*t]*
       Flap[STalbot[r, (2 i - 1) \[Theta]]]*
       dsda[r, (2 i - 1) \[Theta]]]   )
    
f[s_] := 1/(s - 1)
TableForm[
 Table[{n, N[simpson[0, \[Pi], n]]}, {n, 10, 100, 10}, 
  TableHeadings -> {{}, {"n", "s_n"}}]]

Can you please help me to accomplish this. I was failed to perform the last code. Please comment if there's something unclear about my post. Thanks in advance!

I have a mathematica code for solving numerical inverse Laplace transform (Credit to Mr. Patrick O. Kano), but sadly the code is using Nintegrate, and i want to use Simpson's rule with adjustable stepsize, and function. By the way my numerical ILT is based on Talbot's method. For more details about Talbot's method, parametrization and its contour, i've mentioned it on my previous question here: Need help to plot Talbot's Contour (Newbie question)

Hope this doesn't sound complicated. So here is my Mathematica code for numerical ILT.

Timeval = 2;
Rval = 2;
Flap[s_] = 1/(s - 1);
Tfunexact[t_] = Exp[1*t];
Valexact = N[Tfunexact[Timeval], 10]

STalbot[r_, a_] = r*a*Cot[a] + I*r*a;
dsda[r_, a_] = (1 + I*(a + Cot[a]*(a*Cot[a] - 1)));
TimeDfun[r_, t_] = 
  Rval/(Pi)*
   NIntegrate[
    Exp[STalbot[r, a]*t]*Flap[STalbot[r, a]]*dsda[r, a], {a, 0, Pi}, 
    WorkingPrecision -> 10];

{Timeval, Approxval} = Timing[TimeDfun[Rval, Timeval]]
RelError = Abs[Approxval - Valexact]/Valexact

Notice the Nintegrate there, i want to replace it with Simpson's rule, and here is my attempt [New Edit]:

simp[r_, t_, 
  M_] := (r/(3 M)) (r*Exp[r*t]*Flap[r] +  
    2*Sum[Exp[
       t*STalbot[r, 2 i*Pi/M]*Flap[STalbot[r, 2 i*Pi/M]]*
        dsda[r, 2 i*Pi/M]], {i, 1, M/2 - 1}] +  
    4*Sum[Exp[
       t*STalbot[r, (2 i - 1)*Pi/M]*Flap[STalbot[r, (2 i - 1)*Pi/M]]*
        dsda[r, (2 i - 1)*Pi/M]], {i, 1, M/2 }])

Flap[s_] := 1/(s - 1);
TableForm[Table[{M, N[simp[0, Pi, M]]}, {M, 10, 100, 10}], 
 TableHeadings -> {{}, {"M", "s_n"}}]

{Timeval, Approxval} = 
 Timing[Re[simp[Rval, Timeval, 50, WorkingPrecision -> 10]]]
RelError = Abs[Approxval - Valexact]/Valexact

Can you please help me to accomplish this. Now, my code shows the result, but still the result was incorrect. Please comment if there's something unclear about my post. Thanks in advance!

added 78 characters in body; edited body
Source Link
user516076
  • 373
  • 1
  • 8

I have a mathematica code for solving numerical inverse Laplace transform (Credit to Mr. Patrick O. Kano), but sadly the code is using Nintegrate, and i want to use Simpson's rule with adjustable stepsize, and function. By the way my numerical ILT is based on Talbot's method. For more details about Talbot's method, parametrization and its contour, i've mentioned it on my previous question here: Need help to plot Talbot's Contour (Newbie question)

Hope this doesn't sound complicated. So here is my Mathematica code for numerical ILT.

Timeval = 2;
Rval = 2;
Flap[s_] = 1/(s - 1);
Tfunexact[t_] = Exp[1*t];
Valexact = N[Tfunexact[Timeval], 10]

STalbot[r_, a_] = r*a*Cot[a] + I*r*a;
dsda[r_, a_] = (1 + I*(a + Cot[a]*(a*Cot[a] - 1)));
TimeDfun[r_, t_] = 
  Rval/(Pi)*
   NIntegrate[
    Exp[STalbot[r, a]*t]*Flap[STalbot[r, a]]*dsda[r, a], {a, 0, Pi}, 
    WorkingPrecision -> 10];

{Timeval, Approxval} = Timing[TimeDfun[Rval, Timeval]]
RelError = Abs[Approxval - Valexact]/Valexact

Notice the Nintegrate there, i want to replace it with Simpson's rule, and here is my attempt:

Clear[a, b, n]
simpson[a_, b_simpson[r_, 
  n_]t_] := (1Rval/(3 n)) Sum[( 
   f[ar*Exp[r t]*Flap[r] + ( 
    2 Sum[Exp[STalbot[r, 2 i -\[Theta]]*t]*Flap[STalbot[r, 2)\[Theta]]]*
 (b - a)/n] + 4 f[a dsda[r, \[Theta]]] +  
    4 Sum[Exp[STalbot[r, (2 i - 1) (b - a)/n]\[Theta]]*t]*
 + 
     f[a +Flap[STalbot[r, (2 i (b - a1)/n], {i,\[Theta]]]*
 1      dsda[r, n/(2}] (bi - a/n1)

TimeDfun[r_, t_]\[Theta]]] :=  )
 Exp[STalbot[r, \[Theta]]*t]*Flap[STalbot[r, \[Theta]]]* 
f[s_] := dsda[r,1/(s \[Theta]]
- 1)
TableForm[
 Table[{n, N[simpson[0, \[Pi], n]]}, {n, 10, 100, 10}, 
  TableHeadings -> {{}, {"n", "s_n"}}]]]

Can you please help me to accomplish this. I was failed to perform the last code. Please comment if there's something unclear about my post. Thanks in advance!

I have a mathematica code for solving numerical inverse Laplace transform (Credit to Mr. Patrick O. Kano), but sadly the code is using Nintegrate, and i want to use Simpson's rule with adjustable stepsize, and function. By the way my numerical ILT is based on Talbot's method. For more details about Talbot's method, parametrization and its contour, i've mentioned it on my previous question here: Need help to plot Talbot's Contour (Newbie question)

Hope this doesn't sound complicated. So here is my Mathematica code for numerical ILT.

Timeval = 2;
Rval = 2;
Flap[s_] = 1/(s - 1);
Tfunexact[t_] = Exp[1*t];
Valexact = N[Tfunexact[Timeval], 10]

STalbot[r_, a_] = r*a*Cot[a] + I*r*a;
dsda[r_, a_] = (1 + I*(a + Cot[a]*(a*Cot[a] - 1)));
TimeDfun[r_, t_] = 
  Rval/(Pi)*
   NIntegrate[
    Exp[STalbot[r, a]*t]*Flap[STalbot[r, a]]*dsda[r, a], {a, 0, Pi}, 
    WorkingPrecision -> 10];

{Timeval, Approxval} = Timing[TimeDfun[Rval, Timeval]]
RelError = Abs[Approxval - Valexact]/Valexact

Notice the Nintegrate there, i want to replace it with Simpson's rule, and here is my attempt:

Clear[a, b, n]
simpson[a_, b_, 
  n_] := (1/3) Sum[
   f[a + (2 i - 2) (b - a)/n] + 4 f[a + (2 i - 1) (b - a)/n] + 
     f[a + 2 i (b - a)/n], {i, 1, n/2}] (b - a/n)

TimeDfun[r_, t_] := 
 Exp[STalbot[r, \[Theta]]*t]*Flap[STalbot[r, \[Theta]]]*
  dsda[r, \[Theta]]

TableForm[
 Table[{n, N[simpson[0, \[Pi], n]]}, {n, 10, 100, 10}, 
  TableHeadings -> {{}, {"n", "s_n"}}]

Can you please help me to accomplish this. I was failed to perform the last code. Please comment if there's something unclear about my post. Thanks in advance!

I have a mathematica code for solving numerical inverse Laplace transform (Credit to Mr. Patrick O. Kano), but sadly the code is using Nintegrate, and i want to use Simpson's rule with adjustable stepsize, and function. By the way my numerical ILT is based on Talbot's method. For more details about Talbot's method, parametrization and its contour, i've mentioned it on my previous question here: Need help to plot Talbot's Contour (Newbie question)

Hope this doesn't sound complicated. So here is my Mathematica code for numerical ILT.

Timeval = 2;
Rval = 2;
Flap[s_] = 1/(s - 1);
Tfunexact[t_] = Exp[1*t];
Valexact = N[Tfunexact[Timeval], 10]

STalbot[r_, a_] = r*a*Cot[a] + I*r*a;
dsda[r_, a_] = (1 + I*(a + Cot[a]*(a*Cot[a] - 1)));
TimeDfun[r_, t_] = 
  Rval/(Pi)*
   NIntegrate[
    Exp[STalbot[r, a]*t]*Flap[STalbot[r, a]]*dsda[r, a], {a, 0, Pi}, 
    WorkingPrecision -> 10];

{Timeval, Approxval} = Timing[TimeDfun[Rval, Timeval]]
RelError = Abs[Approxval - Valexact]/Valexact

Notice the Nintegrate there, i want to replace it with Simpson's rule, and here is my attempt:

Clear[a, b, n]
simpson[r_, 
  t_] := (Rval/(3 n)) ( 
   r*Exp[r t]*Flap[r] +  
    2 Sum[Exp[STalbot[r, 2 i \[Theta]]*t]*Flap[STalbot[r, \[Theta]]]*
       dsda[r, \[Theta]]] +  
    4 Sum[Exp[STalbot[r, (2 i - 1) \[Theta]]*t]*
       Flap[STalbot[r, (2 i - 1) \[Theta]]]*
       dsda[r, (2 i - 1) \[Theta]]]   )
    
f[s_] := 1/(s - 1)
TableForm[
 Table[{n, N[simpson[0, \[Pi], n]]}, {n, 10, 100, 10}, 
  TableHeadings -> {{}, {"n", "s_n"}}]]

Can you please help me to accomplish this. I was failed to perform the last code. Please comment if there's something unclear about my post. Thanks in advance!

deleted 292 characters in body; edited title; added 3 characters in body
Source Link
user516076
  • 373
  • 1
  • 8

Need help to translate MATLAB code to Mathematica and replace Nintegrate with Simpson's Rule

I have a mathematica code for solving numerical inverse Laplace transform (Credit to Mr. Patrick O. Kano), but sadly the code is using Nintegrate, and i want to use Simpson's rule with adjustable stepsize, and function. But i have a MATLAB code for Simpson's rule, and i need your help to translate the MATLAB code to the Mathematica code for replacing "Nintegrate". By the way my numerical ILT is based on Talbot's method. For more details about Talbot's method, parametrization and its contour, i've mentioned it on my previous question here: Need help to plot Talbot's Contour (Newbie question)

Hope this doesn't sound complicated. So here is my Mathematica code for numerical ILT.

Timeval = 2;
Rval = 2;
Flap[s_] = 1/(s - 1);
Tfunexact[t_] = Exp[1*t];
Valexact = N[Tfunexact[Timeval], 10]

STalbot[r_, a_] = r*a*Cot[a] + I*r*a;
dsda[r_, a_] = (1 + I*(a + Cot[a]*(a*Cot[a] - 1)));
TimeDfun[r_, t_] = 
  Rval/(Pi)*
   NIntegrate[
    Exp[STalbot[r, a]*t]*Flap[STalbot[r, a]]*dsda[r, a], {a, 0, Pi}, 
    WorkingPrecision -> 10];

{Timeval, Approxval} = Timing[TimeDfun[Rval, Timeval]]
RelError = Abs[Approxval - Valexact]/Valexact

Notice the Nintegrate there, i want to replace it with Simpson's rule, and here is my MATLAB code that need to be translated in order to use in my Mathemathica codeattempt:

clearClear[a, all;
clc;
a=0;
b=pi;b, n]
n=input('nsimpson[a_, partitionsb_, (must 
 be even)n_] := ');
tic
f=@(x)sin(x1/3); Sum[
d=quad   f[a + (f,a,b2 i - 2);
h= (b - a)/n;
fprintf('Exactn] Value+ =4 %8.8f\n',d);
dispf[a + (['Step2 sizei =- ',num2str(h)]);
I=f(a1)+f (b);
y=0;
for i=1:n-1
  a)/n] + x=a+i*h;
    iff[a mod(i,+ 2)==1
 i (b - a)/n], {i, 1, n/2}] y=y+4*f(xb - a/n); 

TimeDfun[r_, t_] :=  
 elseExp[STalbot[r, \[Theta]]*t]*Flap[STalbot[r, \[Theta]]]*
  dsda[r, \[Theta]]

TableForm[
 Table[{n, N[simpson[0, \[Pi], n]]}, y=y+2*f(x);
{n, 10, 100, 10}, end
end
L=(I+y)*h/3;
error=abs(d-L);
fprintf('Simpson result= %8.15f\n'TableHeadings -> {{},L);
disp(['Error= '{"n",num2str(error) "s_n"}}]);
toc

Can you pleasaeplease help me to accomplish this. I was failed to perform the last code. Please comment if there's something unclear about my post. Thanks in advance!

Need help to translate MATLAB code to Mathematica and replace Nintegrate with Simpson's Rule

I have a mathematica code for solving numerical inverse Laplace transform (Credit to Mr. Patrick O. Kano), but sadly the code is using Nintegrate, and i want to use Simpson's rule with adjustable stepsize, and function. But i have a MATLAB code for Simpson's rule, and i need your help to translate the MATLAB code to the Mathematica code for replacing "Nintegrate". By the way my numerical ILT is based on Talbot's method. For more details about Talbot's method, parametrization and its contour, i've mentioned it on my previous question here: Need help to plot Talbot's Contour (Newbie question)

Hope this doesn't sound complicated. So here is my Mathematica code for numerical ILT.

Timeval = 2;
Rval = 2;
Flap[s_] = 1/(s - 1);
Tfunexact[t_] = Exp[1*t];
Valexact = N[Tfunexact[Timeval], 10]

STalbot[r_, a_] = r*a*Cot[a] + I*r*a;
dsda[r_, a_] = (1 + I*(a + Cot[a]*(a*Cot[a] - 1)));
TimeDfun[r_, t_] = 
  Rval/(Pi)*
   NIntegrate[
    Exp[STalbot[r, a]*t]*Flap[STalbot[r, a]]*dsda[r, a], {a, 0, Pi}, 
    WorkingPrecision -> 10];

{Timeval, Approxval} = Timing[TimeDfun[Rval, Timeval]]
RelError = Abs[Approxval - Valexact]/Valexact

Notice the Nintegrate there, i want to replace it with Simpson's rule, and here is my MATLAB code that need to be translated in order to use in my Mathemathica code:

clear all;
clc;
a=0;
b=pi;
n=input('n partitions (must be even)= ');
tic
f=@(x)sin(x);
d=quad(f,a,b);
h=(b-a)/n;
fprintf('Exact Value = %8.8f\n',d);
disp(['Step size = ',num2str(h)]);
I=f(a)+f(b);
y=0;
for i=1:n-1
    x=a+i*h;
    if mod(i,2)==1
        y=y+4*f(x);
    else
        y=y+2*f(x);
    end
end
L=(I+y)*h/3;
error=abs(d-L);
fprintf('Simpson result= %8.15f\n',L);
disp(['Error= ',num2str(error)]);
toc

Can you pleasae help me to accomplish this. Please comment if there's something unclear about my post. Thanks in advance!

Need help to replace Nintegrate with Simpson's Rule

I have a mathematica code for solving numerical inverse Laplace transform (Credit to Mr. Patrick O. Kano), but sadly the code is using Nintegrate, and i want to use Simpson's rule with adjustable stepsize, and function. By the way my numerical ILT is based on Talbot's method. For more details about Talbot's method, parametrization and its contour, i've mentioned it on my previous question here: Need help to plot Talbot's Contour (Newbie question)

Hope this doesn't sound complicated. So here is my Mathematica code for numerical ILT.

Timeval = 2;
Rval = 2;
Flap[s_] = 1/(s - 1);
Tfunexact[t_] = Exp[1*t];
Valexact = N[Tfunexact[Timeval], 10]

STalbot[r_, a_] = r*a*Cot[a] + I*r*a;
dsda[r_, a_] = (1 + I*(a + Cot[a]*(a*Cot[a] - 1)));
TimeDfun[r_, t_] = 
  Rval/(Pi)*
   NIntegrate[
    Exp[STalbot[r, a]*t]*Flap[STalbot[r, a]]*dsda[r, a], {a, 0, Pi}, 
    WorkingPrecision -> 10];

{Timeval, Approxval} = Timing[TimeDfun[Rval, Timeval]]
RelError = Abs[Approxval - Valexact]/Valexact

Notice the Nintegrate there, i want to replace it with Simpson's rule, and here is my attempt:

Clear[a, b, n]
simpson[a_, b_,  
  n_] := (1/3) Sum[
   f[a + (2 i - 2) (b - a)/n] + 4 f[a + (2 i - 1) (b - a)/n] + 
    f[a + 2 i (b - a)/n], {i, 1, n/2}] (b - a/n) 

TimeDfun[r_, t_] :=  
 Exp[STalbot[r, \[Theta]]*t]*Flap[STalbot[r, \[Theta]]]*
  dsda[r, \[Theta]]

TableForm[
 Table[{n, N[simpson[0, \[Pi], n]]}, {n, 10, 100, 10}, 
  TableHeadings -> {{}, {"n", "s_n"}}]

Can you please help me to accomplish this. I was failed to perform the last code. Please comment if there's something unclear about my post. Thanks in advance!

Source Link
user516076
  • 373
  • 1
  • 8
Loading