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
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
yuval ohayon 2017년 9월 19일
so the function is [r,count]=dectobin(r,count)? its kittle bit messy for me. so the function iv been writing is good? help me with some lineshelp,and ill try continue from there
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.
  1. 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.
  2. 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
yuval ohayon 2017년 9월 20일
편집: Walter Roberson 2017년 9월 20일
function [x,y] = 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;
end
weel this is the function after adding x,y output
for the main
clear
clc
r=3;
n=2;
drawcircle(r,n)
plot(x,y);
still not working
yuval ohayon
yuval ohayon 2017년 9월 20일
how do i call the function ,i don't know unfortunatly
[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)
Stephen23
Stephen23 2017년 9월 20일
편집: Stephen23 2017년 9월 20일
@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
yuval ohayon 2017년 9월 24일
편집: Walter Roberson 2017년 9월 24일
hi.i succed in makin a circle,but i need to make in the same graph couple of circle with random radius. the problem, is that it keep drawing circle one at a time in figure 1. these is my program .please help :}
clear
clc
r=5;
n=20;
i=1;
x0=0;
y0=0;
t=linspace(0,2*pi,n);
x=r*cos(t)+x0;
y=r*sin(t)+y0;
for i=1:r
xx=x-i;
yy=y-i;
plot(xx,yy);
end
axis equal;
Walter Roberson
Walter Roberson 2017년 9월 24일
  1. Use a function instead of a script. You started with a function and should have stayed with one
  2. use rand() times the maximum permitted radius to generate a random radius
  3. use "hold on" after the first circle, and "hold off" after the last one
yuval ohayon
yuval ohayon 2017년 10월 9일
Hi,how do i generate multiple randi radius ,that the gaps between each circle is the same in the x-y plane.i think i need firdt restart the axises
Walter Roberson
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
yuval ohayon 2017년 10월 10일
Lets say the radius is 5 and n=3.its means i need to build 3 circles.i need also that the gaps maintain the same size.so for this example.the big circle radius 5 after that circlr with the size 3.and last one with radius 1. The gap are 2. 5 to 3 to 1. Understand the consept?

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

질문:

2017년 9월 19일

댓글:

2017년 10월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by