using function in matlab
Show older comments
hi,i want to write a function who get radius and number of circles that should be in the primary circle.
this is what i wrote till now,but still it not call to the function unfortunately.
function [ ] = drawcircle( r,n )
hold on;
for i:1:n
theta=linspace(2*pi/n,2*pi,n);
x=r*cos(theta);
y=r*sin(theta);
plot(y,x)
hold off;
main -
count=3;
r=5;
theta=linspace(0,2*pi);
drawcircle(r,count);
plot(y,x)
Answers (1)
Steven Lord
on 19 Sep 2017
0 votes
In the future, please format your code using the {}Code button. You can select all the code and press that button and it will be automatically formatted.
You or someone else has tagged this homework, but you showed what you'd done. Thank you for showing your work.
You expected to be able to use the x and y variables that were created inside drawcircle outside of that function, but were surprised when that threw an error. Correct? The issue is that each function operates in its own workspace that is its "private play area". Functions generally [*] don't have access to variables and data outside their own workspaces.
If you want a function to work on data from outside, you should pass it into the function as an input argument. If you want to be able to work outside on data created within the function, you should return it from the function as an output argument.
In this case, you're passing data into your drawcircle function. The data stored in the r variable in the "main" workspace from which drawcircle was called is accessible inside drawcircle, and in that function its name is r. Similarly the data stored in count outside is accessible inside, but inside it is known as n. But you're not passing data out of drawcircle. To do that, see the "Output arguments" row of the table in the Syntax for Function Definition section on this documentation page.
Once you've defined drawcircle to return x and y as output arguments, you only need to call drawcircle with two outputs in order for that data to be accessible outside the function.
[*] Yes, I know there are other techniques by which functions can share data other than input and output arguments. This seems like homework for a course where the students are being introduced to MATLAB so I want to keep things simple.
11 Comments
yuval ohayon
on 19 Sep 2017
Steven Lord
on 19 Sep 2017
Let's take a look at an example. In your "main" code you called a function.
theta=linspace(0,2*pi);
Here's the first line of linspace.m that shows how the linspace function is defined.
function y = linspace(d1, d2, n)
The linspace function accepts three input arguments, and inside those arguments are known by the names d1, d2, and n regardless of how they were known outside. In your call to linspace, d1 is 0 and d2 is 2*pi. [*]
The linspace function returns one output argument, the data contained in the variable named y in the linspace workspace. Since you called linspace with a variable named theta as the output, that's how that data is known in your main code.
So you need to make two changes to your code.
- Modify the definition of your drawcircle function to state what you want it to return as output. In this case I suspect those are the x and y variables you created inside drawcircle.
- Modify how you call drawcircle to specify the names by which you want those outputs to be known in your main code. In this case I suspect those are the x and y variables you use when you call plot in your main code.
[*] Internally linspace handles the case where you don't specify n by choosing a default value; how it does that is not important for this explanation.
yuval ohayon
on 20 Sep 2017
Edited: Walter Roberson
on 20 Sep 2017
yuval ohayon
on 20 Sep 2017
Walter Roberson
on 20 Sep 2017
[x, y] = drawcircle(r, n);
Notice, though, that you already plotted inside the function, and then you plot again outside the function.
Notice also that inside you plot(y,x) and outside you plot(x,y)
@yuval ohayon: how to call functions and a lot of other very useful basic MATLAB concepts are covered very well in the Introductory Tutorials, which are highly recommended for all beginners:
These tutorials will not take you long, and afterwards you would know a lot more basic MATLAB usage concepts than you do now, and you would not have wasted pointless days trying to guess how basic MATLAB concepts work, and would not have needed to ask random strangers on the internet for help.
yuval ohayon
on 24 Sep 2017
Edited: Walter Roberson
on 24 Sep 2017
Walter Roberson
on 24 Sep 2017
- Use a function instead of a script. You started with a function and should have stayed with one
- use rand() times the maximum permitted radius to generate a random radius
- use "hold on" after the first circle, and "hold off" after the last one
yuval ohayon
on 9 Oct 2017
Walter Roberson
on 9 Oct 2017
I am not sure what you mean about "the gaps between each circle is the same in the x-y plane" ? Could you give a sample diagram?
yuval ohayon
on 10 Oct 2017
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!