I have the following code in Mathematica using the Finite difference method to solve for c1(t), where . However, I am having trouble writing the sum series in Matlab. The attatched image shows how the plot of real(c(t) should look like.
\[CapitalOmega] = 0.3;
\[Alpha][\[Tau]] := Exp[I \[CapitalOmega] \[Tau]] ;
dt = 0.1;
Ns = 1000;
ds = dt/Ns;
Ttab = Table[T, {T, 0, 10, dt}];
Stab = Table[s, {s, 0, dt - ds, ds}];
c[0] = 1;
Do[corrSum[n] = Sum[c[nn - 1]*Sum[\[Alpha][n dt - m ds]*ds, {m, Ns (nn - 1), Ns nn , 1}], {nn, 1, n}];
c[n] = c[n - 1] - dt*corrSum[n](*c[n-1]*\[Alpha][n dt]*), {n, 1, 100}]
cTtab = Table[{n*dt, c[n]}, {n, 0, 100}]
FDiff = ListPlot[Re[cTtab], PlotStyle -> Orange, PlotLegends -> {"Finite Difference"}]

 採用された回答

Alan Stevens
Alan Stevens 2020 年 11 月 7 日

0 投票

By differentiating c' again you can solve the resulting second order ode as follows
c0 = 1;
v0 = 0;
IC = [c0 v0];
tspan = [0 10];
[t, C] = ode45(@odefn, tspan, IC);
c = C(:,1);
plot(t,real(c),'ro'),grid
xlabel('t'), ylabel('real(c)')
function dCdt = odefn(~,C)
Omega = 0.3;
c = C(1);
v = C(2);
dCdt = [v;
-1i*Omega*v - c];
end
This results in

7 件のコメント

Jose Aroca
Jose Aroca 2020 年 11 月 7 日
Hi Alan. First of all, many thanks for your answer. Indeed, there are many different ways of solving this ODE. It can also be done uisng Laplace transform. The method you show works great, but I need to implement the finite difference method described above, so I can compare the effectiveness of the different methods.
It could be great if you could help me implement the algorith described, as my Matlab skills are not great. Again, thank you for your time!
Alan Stevens
Alan Stevens 2020 年 11 月 7 日
Ok, I didn't realize you were doing a straight comparison. I went for the 2nd order ode approach because the Mathematica finite difference method looked so messy! I'll have a second look, but no promises!
Jose Aroca
Jose Aroca 2020 年 11 月 7 日
Hi, thank you again. it does look quite messy, sorry for that, but it's the only thing I've got. The document attatched could be of help, as it describes the method for this particular expression.
Alan Stevens
Alan Stevens 2020 年 11 月 7 日
Do you have the next page of the pdf?
Jose Aroca
Jose Aroca 2020 年 11 月 7 日
Here you have, but I'm not sure if it will be of any help
Alan Stevens
Alan Stevens 2020 年 11 月 8 日
Well, here is an explicit finite difference version that uses an outer and an inner loop, with the same values of dt and ds as in the Mathematica version. However, it is probably not what you want still, as it isn't a line by line translation of the Mathematica code. I'll leave further development to you!
Omega = 0.3;
dt = 0.1;
Ns = 1000;
ds = dt/Ns;
t = 0:dt:10;
N = numel(t);
c = zeros(1,N);
c(1) = 1;
v = 0;
for n = 1:N-1 % Outer loop; c is updated each iteration of this loop
% Inner loop: c is taken to be constant through this loop, the same
% approximation as is made in section 5.2.1 of the pdf.
for nn = 1:Ns
v = v - (1i*Omega*v + c(n))*ds;
end
c(n+1) = c(n) + v*dt;
end
plot(t,real(c),'.'),grid
xlabel('t'),ylabel('real(c)')
Jose Aroca
Jose Aroca 2020 年 11 月 9 日
Hi, I think that I should be able to work with this. Thank you very much for your help!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeProgramming についてさらに検索

製品

リリース

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by