Very basic calculations returning incorrect values

So I'm trying to run some very, very basic equations through Matlab, and I'm getting absurd values for some reason. Here is a sample of the code I'm trying to run:
r1 = 0.1;
r2 = 0.12;
r3 = 0.14;
h1 = 0.200;
h2 = 0.240;
x = r2-r1;
A1 = pi*square(r2) ;
V1 = pi*square(r1)*h1 ;
V2 = pi*square(r2)*h2 ;
Vt = V2-V1 ;
V3 = pi*square(r3)*h2 ;
Vi = V3-V2;
However the value returned by Matlab for A1 is 3.1416, when it should be more like 0.045. The same thing happens for V1, V2, and V3. Also for some reason, the values returned for V2 and V3 are both 0.754, which is kind of odd. I'm very new to this and I'm sure I'm just making some silly mistake. Any help would be appreciated.

1 件のコメント

Stephen23
Stephen23 2018 年 2 月 5 日
編集済み: Stephen23 2018 年 2 月 5 日
square returns a square wave. You need to use x^2 or x.^2:
or simply x.*x

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

 採用された回答

Jan
Jan 2018 年 2 月 5 日
編集済み: Jan 2018 年 2 月 5 日

2 投票

Of course the results are exactly, what they are expected to be. I assume, you expectations to the function square are wrong. Please read the documentation: doc square.
r2 = 0.12;
square(r2)
% >> 1
A1 = pi*square(r2)
% A1: 3.14159265
square does not the square the value of the argument. I guess you mean:
A1 = pi * r2 ^ 2
This replies 0.0452 .

2 件のコメント

Oliver Green
Oliver Green 2018 年 2 月 5 日
Wow, I feel like a fool. Thank you so much
John D'Errico
John D'Errico 2018 年 2 月 5 日
Well, you were not entirely to blame. I'd say that it is easy to presume square is a function that squares a number. It makes perfect sense to me.
The learning point is to not assume you know what a function does without reading the help for that function. Look at the examples provided, etc. For example, a common mistake that people make is that sin(30) should return 1/2. Of course that fails, since sin uses arguments in radians, not degrees. So always read the help for tools that you will use. It will save you a great deal of debugging time in the long run.

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

その他の回答 (1 件)

Torsten
Torsten 2018 年 2 月 5 日
編集済み: Torsten 2018 年 2 月 5 日

1 投票

Use x^2 instead of square(x) (square is not what you expect it to be in MATLAB).
Best wishes
Torsten.

カテゴリ

ヘルプ センター および File ExchangeDesktop についてさらに検索

質問済み:

2018 年 2 月 5 日

コメント済み:

2018 年 2 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by