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 Kommentar

Walter Roberson
Walter Roberson am 8 Dez. 2015
No, s should be a scalar, not a 1000 x 1 vector.
To define t, use the ':' operator.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Geoff Hayes
Geoff Hayes am 8 Dez. 2015

0 Stimmen

Krish - your t will be defined given the minimum, maximum, and step size. For example, you can create t as a linearly spaced vector of elements using linspace as
t = linspace(minValue,maxValue,numValues);
Note the change in variable names. Never name your variables using MATLAB built-in functions (like min and max).
So now that you have your t, you will be able to determine f. But look closely at your definition for it
f=e^(cos(t))-2cos(4t)-(sin(t/12))^5;
What is e? Do you mean to use exp instead? What is 4t or 2cos? MATLAB will not be able to evaluate these expressions. I will leave it to you to figure out what is wrong and how to correct these statements. Note that the MATLAB editor should be flagging them as incorrect (underlined in red) so make use of these "hints" that something is wrong with the code. (For the same reason, there will be problems with the expressions for x and y.)
Also, why is s a 1000x1 array? Go back to the homework assignment and ask yourself what should s be. Also, note the output of butterfly - what should it be and are you providing that?
The values of x0 and y0 will be the centre because that is how you they are being used in the expressions for x and y. This happens due to the nature of these equations, so there is nothing there for you to do except evaluate for x and y.
Finally, you will probably also observe a problem when evaluating
f*sint
f will be an array of values, sint will be an array of values (ignore for the moment that sint cannot be evaluated as is), so what should this product be? Element-wise multiplication or something else?
Once you get this working, you should observe a very nice outline of a butterfly. Increasing the n (the number of steps) should provide a smoother (more accurate) representation.

7 Kommentare

Krish Desai
Krish Desai am 8 Dez. 2015
I'm trying to make s a random number between 0 and 1, how do I do this?
I don't know what to do for f*sin(t). I understand why you're saying it won't work, because t is a changing value. How am I supposed to fix this?
Krish - to generate a single random number between 0 and 1, just do
s = rand();
With
s = rand(1000,1);
you are creating 1000 random numbers!
As for
f*sint
the problem is with the sint. What is this? It isn't a local variable so what do you really mean with this line of code. Is it
f*sin(t)
which is better where t is an array of elements. But then so is f. So how do you multiply the two together? (Re-read my answer.)
function out=butterfly(minval,maxval,numval)
x0=input('Input the x value of the butterfly center');
y0=input('Input the y value of the butterfly center');
s=rand(1);
for t=minval:numval:maxval
f=exp(cos(t))-2*cos(4*t)-(sin(t/12))^5;
x=x0+s*f*sin(t);
y=y0+s*f*cos(t);
end
plot(x,y,'b.');
hold on
end
Alright, I got to here, I think everything is fixed but it only plots one blue dot. Do you see anything wrong with the code?
In your loop,
x=x0+s*f*sin(t);
you are overwriting all of x. After your loop, x and y will be scalars.
so instead I need
x = [x; x0 + s*f*sin(t)]
Walter Roberson
Walter Roberson am 10 Dez. 2015
That would be one approach.
Krish Desai
Krish Desai am 10 Dez. 2015
Figured something else out, using a counter. Resolved. Thanks.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Shivam Chaturvedi
Shivam Chaturvedi am 8 Dez. 2015

0 Stimmen

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
John BG am 8 Dez. 2015

0 Stimmen

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

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by