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 comentarios

John D'Errico
John D'Errico el 19 de Nov. de 2020
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
Syed Masroor Hussain el 19 de Nov. de 2020
I understand, thank you. So I should not focus on this problem for now?
dpb
dpb el 19 de Nov. de 2020
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
Syed Masroor Hussain el 19 de Nov. de 2020
Thank you for the helpful insight. I should've been mucn more careful.
The issue however is in the display of the answer itself, logically I should get an answer of 3.141 when I enter the value of radius as 1, but that's not the case. I get something like 7.30 a bunch of numbers and e+03, and that is what bugs me the most. I've even reinstalled the software in case there's something wrong with it.
I should definitely look up instructor manuals and Youtube guides as John suggested. But sadly our teacher's interest is basically next to naught. She says stuff like your PC/laptop is messed up :/
dpb
dpb el 19 de Nov. de 2020
Re-read the above in detail. Your radius internally is NOT a numeric one, it's double('1') --> 49
The documentaion explains why.

Iniciar sesión para comentar.

Respuestas (1)

Steven Lord
Steven Lord el 19 de Nov. de 2020

0 votos

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 comentarios

Syed Masroor Hussain
Syed Masroor Hussain el 19 de Nov. de 2020
Thank you. Honestly our teacher just wrote the syntax on the board and expected us to do the rest of the work. I came home and tried it, and got the value of 7 point some crazy numbers with the exponential, even though all I was trying to do was multiply pi with the square of 1.
I've corrected my mistake of the missing ' thanks to the above comments. And yet, same result.
dpb
dpb el 19 de Nov. de 2020
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'
r = '1'
x = r^2
x = 2401
double(r)
ans = 49
The char array '1' is not the same as the double array 1.

Iniciar sesión para comentar.

Categorías

Más información sobre Get Started with MATLAB en Centro de ayuda y File Exchange.

Productos

Preguntada:

el 19 de Nov. de 2020

Comentada:

el 19 de Nov. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by