Flowcharts for loops 1 Using a for loop write a MatLab scri
Flowcharts & for loops
1. Using a for loop, write a MatLab script to convert ounces to grams for 0 to 16 ounces, in 1-ounce increments. Present output in a table; label columns appropriately.
2.
A Fibonacci sequence is composed of elements created by adding the previous 2 elements. The simplest Fibonacci sequence starts with 1, 1 and proceeds as follows:
1,1,2,3,5,8…
Element 3 is the sum of elements 1 and 2 (1+1=2)
Element 4 is the sum of elements 2 and 3 (1+2=3)
Or in the general case, element(i)=element(i-1)+element(i-2)
a. Use a for loop to write a MatLab script to create a 25 element Fibonacci sequence. NOTE: you will need to define the first 2 elements.
b. Create a data table with the element number and the associated Fibonacci number.
c. Plot the results using a polar coordinate plot where the element number is angle (theta) and the associated Fibonacci number is the radius (rho).
Solution
2.
Create a function file named by radius:
function [ result ] = radius( theta )
if theta==0||theta==1
result = theta;
else
result = radius(theta -2)+radius(theta -1);
end
end
And write the code below to your command window:
for theta = 0:25
fprintf(\'Fibonacci(%d)= %d\ \', theta, radius(theta));
% use Matlab\'s polar command
polar(theta,radius)
end
