Writing a "circle" function
9 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Charlie Harris
el 14 de Ag. de 2016
Comentada: Image Analyst
el 14 de Ag. de 2016
The assignment: "write a circle that takes a scalar input r. It needs to return an output called area that is the area of a circle with radius r and a second output, cf that is the circumference of the circle. Use built-in function pi.
My answer:
function[area,cf] = circle(r)
% AREA and CIRCUMFERENCE of a circle, where r = radius of the circle.
r = rand;
cf = 2*pi*r;
area = pi*r.^2;
The "graders" response: "NOTE: the grader will only determine if your
solution for Problem 1 is correct or not. No score will be given.
Problem 1 (circle):
Testing with argument(s) 1
Feedback: Your function made an error for argument(s) 1
Your solution is _not_ correct."
I assumed r = rand was incorrect, however, now I think it means line(1).
Can anyone point me in the right direction?
0 comentarios
Respuesta aceptada
Image Analyst
el 14 de Ag. de 2016
Yes, they're both incorrect because it's the same thing. Overwriting the user's input value of r is incorrect. That is line 1 of the function. Why did you do that? Nowhere in the assignment did it say anything about getting a random number so why did you use rand()? How can you possibly give the correct answer to the user when you totally ignored the radius they wanted to use and just replaced it with a random number?
2 comentarios
Image Analyst
el 14 de Ag. de 2016
You can create a value for r in whatever way you want. You can use rand(), you can just assign a value, you can use input() or inputdlg() to ask the user, you can read it from a file, or whatever. But once you have the value that you want to pass into your function, you can't then change the value inside the function and expect to get the same answer as if you didn't change it. You have to understand the concept of input variable declared in the function definition and variables in the main calling routine. They don't even need to have the same name. But once you have passed in a value, it takes on that value in the function. For example, I can have test.m be this
function test()
rMain = 42;
[areaMain, circMain] = CalcCircArea(rMain)
function [areaOut, circOut] = CalcCircArea(rIn)
areaOut = pi * rIn .^2;
circOut = 2 * pi * rIn;
Those can be in a single m-file or in two m-files. Now, you can set rMain equal to rand if you want or ask the user for it - whatever you want. But you need to pass rMain into CalcCircArea() via the argument list. However, inside CalcCircArea(), the variable rIn will take on the value of rMain. It doesn't matter that they have different names - it will take on the value you passed in.
Essentially what you originally did was to overwrite rIn to be a random number. So rIn originally had the value 42 (in my example) and now you replaced it with some number between 0 and 1. So how can you return the correct circumference and area when you're using a value os (say) 0.2343245234 instead of 42? You can't.
Another thing to know is "pass by value" and "pass by reference" - a fundamental concept in computer programming. MATLAB is pass by value. So you're just passing the value of rMain into CalcCircArea(), not the actual variable (it's memory location). So if you, inside the function, set rIn to some other number, then in the main function after you call CalcCircArea(), rMain will not be affected. The function changed the value but only internally - the changed value will not survive with its new changed value once you leave the function. The only way to do that would be to also return rIn via the output argument list and to accept that value back into rMain. If MATLAB had been "pass by reference" then it would have changed the value or rMain even without passing it back out. But it's not that way. Anyway, that was not related to your problem but I can see that it's something you don't yet know about but do need to know about.
Más respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!