Function that graphs equations
显示 更早的评论
Create a function named butterfly. The function receives three input arguments and returns two output variables (x and y values). The function body should also contain a plot command (y as a function of x.
Use loops to plot the butterfly using the equations:
f=e^(cos(t))-2cos(4t)-(sin(t/12))^5
x=x0+s*f*sint
y=y0+s*f*cost
The three input arguments define the function t: minimum value, maximum value and step size.
The variable s is used to scale the height of the butterfly. It should be defined inside the body of the function as a random real number between 0 and 1 (s is not an input argument for the function).
The variables x0 and y0 define the location of the butterfly center. Ask the user to input their values inside the body of the function.
What I have so far:
function out=butterfly(min,max,n)
x0=input('Input the x value of the butterfly center')
y0=input('Input the y value of the butterfly center')
s=rand(1000,1)
x=x0+s*f*sint
y=y0+s*f*cost
plot(x,y)
How do I (1) get the values of x0,y0 to be the center and (2) define t with the values given.
1 个评论
Walter Roberson
2015-12-8
No, s should be a scalar, not a 1000 x 1 vector.
To define t, use the ':' operator.
采纳的回答
更多回答(2 个)
Shivam Chaturvedi
2015-12-8
You can define t in the following way:
t = min:n:max % will define values from min to max with step size of n
You should be able to do the calculations as mentioned in your script thereafter.
Also use the .* operator instead of the * operator to multiply element-wise instead of matrix multiplication.
Then the equation
x=x0+s*f*sint
will change to:
x=x0+s.*f.*sint
And similarly the others and you should be fine.
John BG
2015-12-8
try
function out=buttnbread(min,max,n)
t=min:n:max
x0=input('Input x center: ')
y0=input('Input y center: ')
f=exp(1)^(cos(t))-2*cos(4*t)-(sin(t/12))^5
s=rand(1000,1)
x=x0+s*f*sin(t)
y=y0+s*f*cos(t)
plot(x,y)
if it doesn't work try reading the question, again
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!