Please assist! Z1= exact solution; Z2= approximate solution. Now i want to plot a 2D graph of the exact and approximate solution in ONE graph to campare the results

1 view (last 30 days)
alpha=1;t=0.004; [x,t] = meshgrid(-4:.1:4, 0.1:.2:0.4); exact=(sech(x/4-t/4)).^2; f0=(sech(x/4)).^2; f1=(sinh(x/4).*(cosh(x/4).^2+1))./(2.*cosh(x/4).^5); f2= ((2.*sinh(x/4)+2.*cosh(x/4).^5+sinh(x/4).^3).*(cosh(x/4).^2+2.*cosh(x/4).^4-5))./(16.*cosh(x/4).^11); f3= (486.*cosh(x/4).^4.*sinh(x/4)-150.*cosh(x/4).^2.*sinh(x/4)-275.*sinh(x/4)+240.*cosh(x/4).^6.*sinh(x/4)-259.*cosh(x/4).^8.*sinh(x/4)+486.*cosh(x/4).^10.*sinh(x/4)-160.*cosh(x/4).^12.*sinh(x/4)-288.*cosh(x/4).^14.*sinh(x/4)-912.*cosh(x/4).^16.*sinh(x/4)+160.*cosh(x/4).^18.*sinh(x/4)+ 128.*cosh(x/4).^20.*sinh(x/4)-850.*cosh(x/4).^5+ 350.*cosh(x/4).^7+1174.*cosh(x/4).^9+514.*cosh(x/4).^1 -668.*cosh(x/4).^13-776.*cosh(x/4).^15+176.*cosh(x/4).^17+128.*cosh(x/4).^19)./(1024.*cosh(x/4).^23); f4=(6873300.*cosh(x/4).^4.*sinh(x/4)-151250.*cosh(x/4).^2.*sinh(x/4)-1739375.*sinh(x/4)+ 660000.*cosh(x/4).^6.*sinh(x/4)-11104474.*cosh(x/4).^8.*sinh(x/4)+17738388.*cosh(x/4).^10.*sinh(x/4)-5089324.*cosh(x/4).^12.*sinh(x/4)-42875520.*cosh(x/4).^14.*sinh(x/4)-3455535.*cosh(x/4).^16.*sinh(x/4)+ 56411614.*cosh(x/4).^18.*sinh(x/4)+31975008.*cosh(x/4).^20.*sinh(x/4)-31724640.*cosh(x/4).^22.*sinh(x/4)- 36843664.*cosh(x/4).^24.*sinh(x/4)+ 13863840.*cosh(x/4).^26.*sinh(x/4)-13521024.*cosh(x/4).^28.*sinh(x/4)+8647168.*cosh(x/4).^30.*sinh(x/4) +13020672.*cosh(x/4).^32.*sinh(x/4)+11695104.*cosh(x/4).^34.*sinh(x/4)- 7403520.*cosh(x/4).^36.*sinh(x/4)-7286784.*cosh(x/4).^38.*sinh(x/4)+1155072.*cosh(x/4).^40.*sinh(x/4)+ 557056.*cosh(x/4).^42.*sinh(x/4)-9583750.*cosh(x/4).^5+8131250*cosh(x/4).^7+30360150.*cosh(x/4).^9-16086950.*cosh(x/4).^11-44329262.*cosh(x/4).^13+20818214.*cosh(x/4).^15+14048786.*cosh(x/4).^17-4409026.*cosh(x/4).^19-19030068*cosh(x/4).^21+46003792.*cosh(x/4).^23+10034944.*cosh(x/4).^25- 46183616.*cosh(x/4).^27-11517056.*cosh(x/4).^29+16566784.*cosh(x/4).^31+3445248.*cosh(x/4).^33+ 4956672.*cosh(x/4).^35-1250304.*cosh(x/4).^37+4407296.*cosh(x/4).^39-6324224.*cosh(x/4).^41+ 262144.*cosh(x/4).^43+262144.*cosh(x/4).^45)./(4194304.*cosh(x/4).^47); u4=f0+f1.*((t.^alpha)/gamma(1+alpha))+f2.*((t.^(2.*alpha))/gamma(1+2.*alpha))+f3.*((t.^(3.*alpha))/gamma(1+3.*alpha))+f4.*((t.^(4.*alpha))/gamma(1+4.*alpha)); Z1=exact;Z2=u4; plot(t,x,Z1) xlabel('x') ylabel('u')

Accepted Answer

Star Strider
Star Strider on 31 Jul 2021
Edited: Star Strider on 31 Jul 2021
Try this:
alpha=1;
t=0.004;
[x,t] = meshgrid(-4:.1:4, 0.1:.2:0.4);
exact=(sech(x/4-t/4)).^2;
f0=(sech(x/4)).^2;
f1=(sinh(x/4).*(cosh(x/4).^2+1))./(2.*cosh(x/4).^5);
f2= ((2.*sinh(x/4)+2.*cosh(x/4).^5+sinh(x/4).^3).*(cosh(x/4).^2+2.*cosh(x/4).^4-5))./(16.*cosh(x/4).^11);
f3= (486.*cosh(x/4).^4.*sinh(x/4)-150.*cosh(x/4).^2.*sinh(x/4)-275.*sinh(x/4)+240.*cosh(x/4).^6.*sinh(x/4)-259.*cosh(x/4).^8.*sinh(x/4)+486.*cosh(x/4).^10.*sinh(x/4)-160.*cosh(x/4).^12.*sinh(x/4)-288.*cosh(x/4).^14.*sinh(x/4)-912.*cosh(x/4).^16.*sinh(x/4)+160.*cosh(x/4).^18.*sinh(x/4)+ 128.*cosh(x/4).^20.*sinh(x/4)-850.*cosh(x/4).^5+ 350.*cosh(x/4).^7+1174.*cosh(x/4).^9+514.*cosh(x/4).^1 -668.*cosh(x/4).^13-776.*cosh(x/4).^15+176.*cosh(x/4).^17+128.*cosh(x/4).^19)./(1024.*cosh(x/4).^23);
f4=(6873300.*cosh(x/4).^4.*sinh(x/4)-151250.*cosh(x/4).^2.*sinh(x/4)-1739375.*sinh(x/4)+ 660000.*cosh(x/4).^6.*sinh(x/4)-11104474.*cosh(x/4).^8.*sinh(x/4)+17738388.*cosh(x/4).^10.*sinh(x/4)-5089324.*cosh(x/4).^12.*sinh(x/4)-42875520.*cosh(x/4).^14.*sinh(x/4)-3455535.*cosh(x/4).^16.*sinh(x/4)+ 56411614.*cosh(x/4).^18.*sinh(x/4)+31975008.*cosh(x/4).^20.*sinh(x/4)-31724640.*cosh(x/4).^22.*sinh(x/4)- 36843664.*cosh(x/4).^24.*sinh(x/4)+ 13863840.*cosh(x/4).^26.*sinh(x/4)-13521024.*cosh(x/4).^28.*sinh(x/4)+8647168.*cosh(x/4).^30.*sinh(x/4) +13020672.*cosh(x/4).^32.*sinh(x/4)+11695104.*cosh(x/4).^34.*sinh(x/4)- 7403520.*cosh(x/4).^36.*sinh(x/4)-7286784.*cosh(x/4).^38.*sinh(x/4)+1155072.*cosh(x/4).^40.*sinh(x/4)+ 557056.*cosh(x/4).^42.*sinh(x/4)-9583750.*cosh(x/4).^5+8131250*cosh(x/4).^7+30360150.*cosh(x/4).^9-16086950.*cosh(x/4).^11-44329262.*cosh(x/4).^13+20818214.*cosh(x/4).^15+14048786.*cosh(x/4).^17-4409026.*cosh(x/4).^19-19030068*cosh(x/4).^21+46003792.*cosh(x/4).^23+10034944.*cosh(x/4).^25- 46183616.*cosh(x/4).^27-11517056.*cosh(x/4).^29+16566784.*cosh(x/4).^31+3445248.*cosh(x/4).^33+ 4956672.*cosh(x/4).^35-1250304.*cosh(x/4).^37+4407296.*cosh(x/4).^39-6324224.*cosh(x/4).^41+ 262144.*cosh(x/4).^43+262144.*cosh(x/4).^45)./(4194304.*cosh(x/4).^47); u4=f0+f1.*((t.^alpha)/gamma(1+alpha))+f2.*((t.^(2.*alpha))/gamma(1+2.*alpha))+f3.*((t.^(3.*alpha))/gamma(1+3.*alpha))+f4.*((t.^(4.*alpha))/gamma(1+4.*alpha));
Z1=exact;
Z2=u4;
figure
hZ1 = plot3(t,x,Z1,'-r');
hold on
hZ2 = plot3(t,x,Z2,'-g');
hold off
grid on
xlabel('x')
ylabel('u')
title('plot3')
legend([hZ1(1),hZ2(1)], 'Z_1','Z_2', 'Location','NE')
figure
hZ1 = plot3(t,x,Z1,'-r');
hold on
hZ2 = plot3(t,x,Z2,'-g');
hold off
grid on
xlabel('x')
ylabel('u')
title('plot3 (2D Version With Rotation)')
view(90,0)
legend([hZ1(1),hZ2(1)], 'Z_1','Z_2', 'Location','best')
figure
surf(t,x,Z1,'FaceAlpha',0.5)
hold on
surf(t,x,Z2,'FaceAlpha',0.5)
hold off
xlabel('x')
ylabel('u')
title('surf')
Please edit your code so that it is readable, with each assignment or command on a separate line. It took a few minutes to go through this and parse it that way so that I could understand it.
EDIT — (31 Jul 2021 at 14:18)
Added ‘plot3 (2D Version With Rotation)’ plot.
.
  5 Comments
Star Strider
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
Z1 = 1×51
0.0157 0.0192 0.0234 0.0286 0.0349 0.0427 0.0521 0.0637 0.0778 0.0950 0.1160 0.1417 0.1731 0.2114 0.2582 0.3153 0.3852 0.4704 0.5746 0.7018 0.8572 1.0470 1.2788 1.5619 1.9077 2.3301 2.8460 3.4761 4.2457 5.1857
Z2=Exact
Z2 = 1×51
0.0157 0.0192 0.0234 0.0286 0.0349 0.0427 0.0521 0.0637 0.0778 0.0950 0.1160 0.1417 0.1731 0.2114 0.2582 0.3153 0.3852 0.4704 0.5746 0.7018 0.8572 1.0470 1.2788 1.5619 1.9077 2.3301 2.8460 3.4761 4.2457 5.1857
Error = Numerical - Exact;
ErrorRMS = sqrt(mean(Error.^2))
ErrorRMS = 1.1945e-14
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.
.

Sign in to comment.

More Answers (2)

Cris LaPierre
Cris LaPierre on 31 Jul 2021
See MATLAB Onramp Ch 9

Omorodion Solomon
Omorodion Solomon on 31 Jul 2021
Yes, you assisted me in solving it last week and I'm grateful. However, I was told to plot a 2D graph of the approximate and exact solution for alpha= 1 and t=0.005. Please I need your assistance again.

Community Treasure Hunt

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

Start Hunting!