Please someone should help me debug my error. The approximate solution is supposed to converge to the exact solution. I think i have problem with the code.
1 view (last 30 days)
Show older comments
Omorodion Solomon
on 21 Aug 2021
Commented: Star Strider
on 21 Aug 2021
i want to plot a graph of the numerical and exact solution. however, the errore is too much. can someone help me to trace my error. the solution are supposed to be ALMOST THE SAME. Here is my code:
alpha=0.25;
r=5;
t=0.002;
x=[-5:0.2:5];
k = reshape(0:100, 1, 1, []);
A=exp(x);
B = (t.^(k.*alpha))./factorial(k).*alpha.^k;
C = sum(B,3);
Numerical=A.*C;
Exact=A.*exp((r-4).*(t.^alpha)./(alpha));
Z1=Numerical;
Z2=Exact;
hZ1 = plot(x,Z1,'-r');
hold on
hZ2 = plot(x,Z2,'-g');
hold off
grid on
xlabel('x')
ylabel('u')
legend([hZ1(1),hZ2(1)], 'CFRDTM','EXACT', 'Location','NE')
0 Comments
Accepted Answer
Star Strider
on 21 Aug 2021
There are several typographical errors, at lelast with respect to the code representing the symbolic equations in the image.
With those corrections, ‘Numerical’ and ‘Exact’ are the same (within floating-point approximation error), at least for the values provided.
alpha=0.25;
r=5;
t=0.002;
x=[-5:0.2:5];
k = reshape(0:100, 1, 1, []);
A=exp(x);
% B =(t.^(k.*alpha))./(factorial(k).*alpha.^k);
B = (t.^(k.*alpha)).*(r-4).^k ./ (factorial(k).*alpha.^k);
C = sum(B,3);
Numerical=A.*C;
% Exact=A.*exp((r-4).*(t.^alpha)./(alpha));
Exact = exp(x+((r-4).*(t.^alpha)./(alpha)));
Z1=Numerical
Z2=Exact
Error = Numerical - Exact;
ErrorRMS = sqrt(mean(Error.^2))
figure
hZ1 = plot(x,Z1,'-r');
hold on
hZ2 = plot(x,Z2,'--g'); % Change To Dashed Line
hold off
grid on
xlabel('x')
ylabel('u')
legend([hZ1(1),hZ2(1)], 'CFRDTM','EXACT', 'Location','NE')
It would also help if the code was parsed into different lines (as I had to do here), in order for it to run.
.
5 Comments
Star Strider
on 21 Aug 2021
The function is part of the code I posted.
Please copy the entire code, then run it, as I do here —
x = [-4,-3,-2,-1,2,3,4];
t = [0.002,0.004,0.006,0.008];
[Xm,Tm] = ndgrid(x,t);
[Exact,Numerical] = runCode(Xm(:),Tm(:));
absErr = abs(Exact-Numerical);
Results = table(Xm(:), Tm(:), Exact, Numerical, absErr, 'VariableNames',{'x','t','Exact','Numerical','AbsoluteError'})
figure
scatter3(Results.x, Results.t, Results.Exact, 'xb')
hold on
scatter3(Results.x, Results.t, Results.Numerical, '+r')
hold off
grid on
xlabel('x')
ylabel('t')
set(gca, 'ZScale','log')
legend('Exact', 'Numerical', 'Location','bestoutside')
function [Exact,Numerical] = runCode(x,t)
alpha=0.25;
r=5;
% t=0.002;
% x=[-5:0.2:5];
k = reshape(0:100, 1, 1, []);
A=exp(x);
% B =(t.^(k.*alpha))./(factorial(k).*alpha.^k);
B = (t.^(k.*alpha)).*(r-4).^k ./ (factorial(k).*alpha.^k);
C = sum(B,3);
Numerical=A.*C;
% Exact=A.*exp((r-4).*(t.^alpha)./(alpha));
Exact = exp(x+((r-4).*(t.^alpha)./(alpha)));
end
.
More Answers (0)
See Also
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!