MATLAB: I am trying to make a golden spiral where I input n So far this is my co
ID: 3829913 • Letter: M
Question
MATLAB: I am trying to make a golden spiral where I input n
So far this is my code:
x = 0;
y = 1;
syms v u
axis off
hold on
for n = 1:8
a = fibonacci(n);
% Define squares and arcs
switch mod(n,4)
case 0
y = y - fibonacci(n-2);
x = x - a;
eqnArc = (v-(x+a))^2 + (u-y)^2 == a^2;
case 1
y = y - a;
eqnArc = (v-(x+a))^2 + (u-(y+a))^2 == a^2;
case 2
x = x + fibonacci(n-1);
eqnArc = (v-x)^2 + (u-(y+a))^2 == a^2;
case 3
x = x - fibonacci(n-2);
y = y + fibonacci(n-1);
eqnArc = (v-x)^2 + (u-y)^2 == a^2;
end
% Draw square
pos = [x y a a];
rectangle('Position', pos)
% Add Fibonacci number
xText = (x+x+a)/2;
yText = (y+y+a)/2;
text(xText, yText, num2str(a))
% Draw arc
interval = [x x+a y y+a];
plot(eqnArc, interval, 'b') %used to be fimplicit
end
with a function file:
function result = fibonacci(n)
if (n == 0) || (n == 1)
result=n;
else
result = fibonacci(n-1) + fibonacci(n-2);
end
end
and it outputs this:
21 13Explanation / Answer
CODE :
x = 0;
y = 1;
syms v u
axis off
hold on
for n = 1:8
a = fibonacci(n);
% Define squares and arcs
switch mod(n,4)
case 0
y = y - fibonacci(n-2);
x = x - a;
eqnArc = (v-(x+a))^2 + (u-y)^2 == a^2;
case 1
y = y - a;
eqnArc = (v-(x+a))^2 + (u-(y+a))^2 == a^2;
case 2
x = x + fibonacci(n-1);
eqnArc = (v-x)^2 + (u-(y+a))^2 == a^2;
case 3
x = x - fibonacci(n-2);
y = y + fibonacci(n-1);
eqnArc = (v-x)^2 + (u-y)^2 == a^2;
end
% Draw square
pos = [x y a a];
rectangle('Position', pos)
% Add Fibonacci number
xText = (x+x+a)/2;
yText = (y+y+a)/2;
text(xText, yText, num2str(a))
% Draw arc
interval = [x x+a y y+a];
fimplicit(eqnArc, interval, 'b')
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.