Area of Circle syntax error
古いコメントを表示
Our teacher had given this syntax for calculatiOn the area of a circle
r=input('enter radius ',s')
a=pi*r^2;
disp(a)
and on my version (R2011a) the answer is being displayed as follows

Note that I'm basically a beginner, not knowing the abcs of it, slowly adopting into the software through the Matlab Demystified book. Can anyone please help me out? Point out the mistake in the syntax or my writing?
5 件のコメント
John D'Errico
2020 年 11 月 19 日
You really just need to spend at least a few minutes reading the manuals, or the getting started tutorials, or a YouTube video explaining how to use MATLAB, or the book that you have. Otherwise you will be asking hundreds of questions, wanting people to rewite those same resources for you.
Syed Masroor Hussain
2020 年 11 月 19 日
dpb
2020 年 11 月 19 日
It's ok to focus on the problem; use the documentation to figure out what is going on.
Note first of all that the above code as posted
r=input('enter radius ',s')
a=pi*r^2;
disp(a)
won't even run--
>> r=input('enter radius ',s')
Error using input
The second argument to INPUT must be 's'.
>>
The first thing you MUST learn and take to heart is that precision is mandatory--missing punctuation and simple typos are simply not acceptable little nits--they may be fatal or even if not as above is, lead to totally wrong answers by referring to a wrong variable or similar.
HINT FOR BEGINNER: Note carefully what the documentation for INPUT says about the form using 's' second input argument:
str = input(prompt,'s') returns the entered text, without evaluating the input as an expression.
So, your r variable is NOT a number but a character string representing the number 1 as text, not as a number. So, what happens when you subsequently perform arithmetic on that character is that MATLAB silently converts the character to its internal representation as a number and carries out the instruction.
>> double(r)
ans =
49
>>
shows that the character '1' is actual the ASCII code number 49 internally; it's the behind the scenes way computers work that makes it show up as the readable digit representing the integer one. If you don't know/understand that, look up ASCII code chart.
Anyways, if you have a value of 49 for the radius internally, it's easy to see that since 50^2 = 2500 that 3*2500 = 7500 which explains why you got 7543 for pi*r^2.
As are student, will leave it to you to carry on to fix the problem -- there are two possible solution paths you can take; which your instructor actually used depends on whether you introduced the 's' argument or did he and you're just missing the punctuation above.
Syed Masroor Hussain
2020 年 11 月 19 日
dpb
2020 年 11 月 19 日
Re-read the above in detail. Your radius internally is NOT a numeric one, it's double('1') --> 49
The documentaion explains why.
回答 (1 件)
Steven Lord
2020 年 11 月 19 日
0 投票
If that's the actual code your teacher gave you, they played a bit of a trick on you. [I assume the missing ' before s was a copy and paste error.] Look at the value returned by input. What data type is it? What value does it contain? Now look at r^2. What type is it and what value does it have? If you're still not sure why that behaves the way it does, use your favorite search engine to learn more about ASCII.
3 件のコメント
Syed Masroor Hussain
2020 年 11 月 19 日
dpb
2020 年 11 月 19 日
Yes. Because of the string as shown in the doc and the example. Look again at what double(r) returns...that's the key.
READ THE DOCUMENTATION CAREFULLY AND FOLLOW THE EXAMPLE CODE ABOVE...the answer is in there.
r = '1'
x = r^2
double(r)
The char array '1' is not the same as the double array 1.
カテゴリ
ヘルプ センター および File Exchange で Get Started with MATLAB についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!