Where is the error in my code?

1 回表示 (過去 30 日間)
Abdallah Qaswal
Abdallah Qaswal 2022 年 6 月 7 日
コメント済み: Voss 2022 年 6 月 7 日
I am trying to plot three variables Q,R,W using surf function, but I get this error message : Error using surf (line 71)
Z must be a matrix, not a scalar or vector.
Error in time1 (line 13)
surf(R,W,Q)
here is my code that I am trying to run:
r = 0:0.07;
w = 0:4;
[R,W] = meshgrid(r,w);
V1 = @(R,W) -acosh(10*(W./(1600*R + 21))^(1/2))/20000000000
V2 = @(R,W) acosh(10*(W./(1600*R + 21))^(1/2))/20000000000
fun = @(x,R,W)0.0018./((W./((cosh(10^10.*x./0.5)).^2)-(R.*16+0.21)).^0.5);
q = @(R,W) integral(@(x)fun(x,R,W),V1(R,W),V2(R,W));
Q = arrayfun(q,R,W)
surf(R,W,Q)
where is the error, please?
Many thanks

採用された回答

Voss
Voss 2022 年 6 月 7 日
Check the value of r:
r = 0:0.07
r = 0
It is a scalar wth value 0 because the colon operator ":" uses an increment of 1 by default. Since 0.07 - 0 < 1, you get just 0.
Perhaps you meant to use a smaller increment:
r = 0:0.01:0.07
r = 1×8
0 0.0100 0.0200 0.0300 0.0400 0.0500 0.0600 0.0700
Then the rest of the code runs:
w = 0:4;
[R,W] = meshgrid(r,w);
V1 = @(R,W) -acosh(10*(W./(1600*R + 21))^(1/2))/20000000000;
V2 = @(R,W) acosh(10*(W./(1600*R + 21))^(1/2))/20000000000;
fun = @(x,R,W)0.0018./((W./((cosh(10^10.*x./0.5)).^2)-(R.*16+0.21)).^0.5);
q = @(R,W) integral(@(x)fun(x,R,W),V1(R,W),V2(R,W));
Q = arrayfun(q,R,W);
surf(R,W,Q)
  2 件のコメント
Abdallah Qaswal
Abdallah Qaswal 2022 年 6 月 7 日
Thank you very much!
It works ! much appreciated!
Voss
Voss 2022 年 6 月 7 日
You're welcome!

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

その他の回答 (2 件)

Les Beckham
Les Beckham 2022 年 6 月 7 日
編集済み: Les Beckham 2022 年 6 月 7 日
You need to specify an increment size if you expect r to be a vector. Currently it is a scalar equal to zero. Q will not be a 2d array if r is a scalar. Thus the error from surf.
r = 0:0.07
r = 0
Maybe you want something like this?
r = 0:0.01:0.07
r = 1×8
0 0.0100 0.0200 0.0300 0.0400 0.0500 0.0600 0.0700
  1 件のコメント
Abdallah Qaswal
Abdallah Qaswal 2022 年 6 月 7 日
Thank you!
it works!

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


Chris
Chris 2022 年 6 月 7 日
編集済み: Chris 2022 年 6 月 7 日
The colon operator has a default spacing of 1.
r = 0:0.07
gives:
0,
% next value...
1 > 0.07 % so r = [0]
Use an intermediate step value, if you want:
r = 0:0.01:0.07;
  1 件のコメント
Abdallah Qaswal
Abdallah Qaswal 2022 年 6 月 7 日
Thank you!
It works now!

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

カテゴリ

Help Center および File ExchangeGraphics Objects についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by