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

조회 수: 1 (최근 30일)
Omorodion Solomon
Omorodion Solomon 2021년 8월 19일
댓글: Walter Roberson 2021년 8월 21일
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')

채택된 답변

Omorodion Solomon
Omorodion Solomon 2021년 8월 21일

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개)

Walter Roberson
Walter Roberson 2021년 8월 19일
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
Walter Roberson
Walter Roberson 2021년 8월 20일
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 2021년 8월 20일
Thank you so much! I'm grateful.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Formatting and Annotation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by