MatLab Looping help please! I have no idea on how to set up a loop for the stack
ID: 672348 • Letter: M
Question
MatLab Looping help please! I have no idea on how to set up a loop for the stacking apples. Help is much appreciated! Background: Your manager at the local supermarket has asked you to create a produce display; he's asked you to build a pyramid of apples from the upcoming shipment. The pyramid should be erected such that each square of four apples in one layer supports one apple on the next layer. Example shown to right: Top layer (1 apple) Next layer (2 rows, 2 columns, i.e., 4 apples) Base layer (3 rows, 3 columns, i.e., 9 apples) www.123rf.com Tasks: You are asked to compute each of the following items. Note: between items, you may or may not choose to duplicate or reuse any of your own code. 1. Given a specified number of layers, how many apples will be used? Show test cases: 5, 7, and 10 layers. Not enough apples are being displayed. Given the dimension of the base of a square pyramid and the total number of apples to be displayed, how many complete, square pyramids can be made? How many apples are left over? Show test cases: 2. a. 5x5 base from 1000 apples, b. 8x8 base from 800 apples These square pyramids would take up too much space to make good displays. You propose making pyramids with rectangular bases. Given the dimensions of the base, how many apples will be used? Show test cases: 3. a. 8x16 base b. 10x32 base 8 6 4. Your manager tells you that you dont need to use all of the apples; the important part is to use as many apples as you can without bruising them. You figure you can probably only stack the apples to a certain height before they will bruise. Given the dimension of the base and the maximum number of layers, how many 5 s0Explanation / Answer
Your Question is too long to finish in two hour. I am only able to finish 3 parts of it, but I hope you can finish fourth part by yourself after looking first three parts. Besides Part 4 is incomplete. I do not wish to answer on guessing the question. :)
Step: Save these codes in a seperate files with the name same as function name and then run the final script at the end of all parts for test cases.
Part 1:
function [appleCount] = apple_sphere(layers);
% Matlab implementation of velocity function
%
%
% input: layers : Number of Layers Required
%
%
% output: appleCount : Number of apples required
%
%
sum = 0;
for i = layers:-1:1
sum = sum + i.^2;
end
appleCount = sum;
Part 2:
function [totalLayer, appleRemaining] = total_apples(dimension, apples);
% Matlab implementation of velocity function
%
%
% input: layers : Number of Layers Required
%
%
% output: appleCount : Number of apples required
%
%
area = dimension.^2;
sum = 0;
applesInBaseLayer = area; % as sphere will be of radius 0.5
totalLayer = sqrt(applesInBaseLayer);
for i = totalLayer:-1:1
sum = sum + i.^2;
end
appleRemaining = apples - sum;
Part 3 :
function [totalApples] = rect_apples(length, width);
% Matlab implementation of velocity function
%
%
% input: layers : Number of Layers Required
%
%
% output: appleCount : Number of apples required
%
%
area = length*width;
sum = 0;
temp = length;
if (length < width)
temp = width;
width = length;
length = temp;
end
applesInBaseLayer = area;
if isequal(length,2)
if isequal(length,2)
totalApples = 2;
else
totalApples = length;
end
else
totalApples = applesInBaseLayer + rect_apples(length-1, width-1);
end
Main Script :
Copy this code in a seperate file and name it anything, and run this file
clear all;
close all;
clc;
n = 5;
appleCount = apple_sphere(n)
n =7;
appleCount = apple_sphere(n)
n =10;
appleCount = apple_sphere(n)
dimension = 5;
apples = 1000;
[totalLayer, appleRemaining] = total_apples(dimension, apples)
dimension = 8;
apples = 800;
[totalLayer, appleRemaining] = total_apples(dimension, apples)
length = 16
width = 8
totalApples = rect_apples(length, width)
%hold on;
[x,y,z] = sphere;
%[2,2,2] = sphere(1);
figure;
surf(x+1,y+1,z+1);
hold on
%surf(x+1,y+1,z+1);
surf(x+3,y+1,z+1);
surf(x+1,y+3,z+1);
surf(x+3,y+3,z+1);
surf(x+2,y+2,z+2);
Note : surf method is used to display spheres, but it is not required in your question.