Can someone please assist me in plotting a 3-D surface?

1 vue (au cours des 30 derniers jours)
Omorodion Solomon
Omorodion Solomon le 19 Août 2021
Commenté : Walter Roberson le 21 Août 2021
I want to plot a 3D graph of the series when alpha=0.25 and and k=1,2,...10.
This is my code but im getting an error :
alpha=0.25;
[x,t] = meshgrid(-4:.1:4, 0.1:.2:0.4);
k=[1:10];
A=exp(x);
B=(t.^(k.*alpha))./factorial(k).*alpha. ^k;
Invalid use of operator.

Error in connector.internal.fevalMatlab

Error in connector.internal.fevalJSON
C=sum(B);
D=A.*C;
mesh(t,x,D)
xlabel('t')
ylabel('x')
zlebel('u_10')

Réponse acceptée

Omorodion Solomon
Omorodion Solomon le 21 Août 2021

The approximate and exact solution are not corresponding. Please assist further. 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')

  1 commentaire
Walter Roberson
Walter Roberson le 21 Août 2021
The formula you asked me to help you plot, as no r in it.

Connectez-vous pour commenter.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 19 Août 2021
alpha=0.25;
[x,t] = meshgrid(-4:.1:4, 0.1:.2:0.4);
k = reshape(1:10, 1, 1, []);
A=exp(x);
B = (t.^(k.*alpha))./factorial(k).*alpha.^k;
C = sum(B,3);
D=A.*C;
mesh(t,x,D)
xlabel('t')
ylabel('x')
zlabel('u_10')
  3 commentaires
Walter Roberson
Walter Roberson le 20 Août 2021
You had
k=[1:10];
B=(t.^(k.*alpha))./factorial(k).*alpha. ^k;
so you obviously wanted to create length(k) = 10 outputs for each input value.
But you have an array of inputs -- t is a 2D array, 81 x 2, and you want to produce length(k) = 10 outputs for each of those 81 x 2 inputs. How do you want to arrange that? Do you want the output to be 10 x 81 x 2? Do you want it to be 81 x 10 x 2 ? Do you want it to be 81 x 2 x 10? Do you want it to be 810 x 2? Do you want it to be 81 x 20?
You followed that with
C=sum(B);
which values are you wanting to sum?
mesh(t,x,D)
as the results are being sent to mesh() with t and x both being 2D arrays, we can see that D is expected to be the same size as t and x are. So with that 81 x 2 input, after the sum of the 10 values per entry, you want an 81 x 2 output. The only way this can make sense is if for each input location, you want to sum the values calculated from the 10 different k entries.
You can do some complicated memory arrangements, but by far the easiest way to handle all of this is to arrange that the output, B, is one of:
  • 10 x 81 x 2 -- in which case you want to sum() along the first dimension to get 1 x 81 x 2 and then collapse the first dimension to get 81 x 2
  • 81 x 10 x 2 -- in which case you want to sum() along the second dimension to get 81 x 1 x 2 and then collapse the second dimension to get 81 x 2
  • 81 x 2 x 10 -- in which case you want to sum() along the third dimension to get 81 x 2 x 1 which will automatically collapse to 81 x 2
sum along the first dimension is sum(B,1) . sum along the second dimension is sum(B,2) . sum along the third dimension is sum(B,3) . sum(B) by itself sums along the first non-singular dimension.
I decided that it was easiest to arrange to sum along the third dimension, as afterwards you do not need to rearrange to get to the 81 x 2 result you need.
The easiest way to get the 10 different k-based results for each location, to get 81 x 2 x 10 from an 81 x 2 input, is to arrange that k is 1 x 1 x 10 instead of the 1 x 10 that you had coded before. After that, the code takes advantage of "implicit expansion"
Omorodion Solomon
Omorodion Solomon le 20 Août 2021
Thank you so much! I'm grateful.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by