using function in matlab
이전 댓글 표시
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)
답변 (1개)
Steven Lord
2017년 9월 19일
0 개 추천
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
yuval ohayon
2017년 9월 19일
Steven Lord
2017년 9월 19일
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
2017년 9월 20일
편집: Walter Roberson
2017년 9월 20일
yuval ohayon
2017년 9월 20일
Walter Roberson
2017년 9월 20일
[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
2017년 9월 24일
편집: Walter Roberson
2017년 9월 24일
Walter Roberson
2017년 9월 24일
- 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
2017년 10월 9일
Walter Roberson
2017년 10월 9일
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
2017년 10월 10일
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!