Azzera filtri
Azzera filtri

MATLAB: Taking one integer input and having it return an array based on the integer inputted

6 visualizzazioni (ultimi 30 giorni)
I'm struggling to write a function which takes one input integer num, and returns an array of the squares of all numbers from 1 to num. The size of the array will vary with the input value given:
so far i have gotten:
y=input('Please enter mass (kg)');
x=input('Please enter height (cm)');
disp(output)
function [output] = BMI(Mass,Height)
output = Mass/Height^2;
end
All help is appreciated<3
Also, the next part of the homework requires me to write a function which takes one input integer num, and returns an array of the squares of all numbers from 1 to num. The size of the array will vary with the input value given, such as:
I haven't made a start on this question as I'm unsure of where to start what code needs to be written to have the input be returned into an array of the square numbers from (1:num), but if anyone can lend a hand on what code would be used to begin and point me in the right direction that would be greatly appreciated!

Risposte (1)

Ameer Hamza
Ameer Hamza il 9 Set 2020
Since this is a homework question so I will not give the exact code, but you will find the following links hints helpful
1) MATLAB function can return multiple inputs and outputs. You can define it like this
function [bmi, bsa] = measurements(mass, height)
bmi = ... % write the formula for bmi
bsa = ... % write the formula for bsa
end
The call the function like this
m = % take mass input from user
h = % take height input from user
[bmi, bsa] = measurements(m, h);
2) You can use element-wise operators to allow operation on each element of a vector: https://www.mathworks.com/help/matlab/ref/power.html

Categorie

Scopri di più su MATLAB in Help Center e File Exchange

Tag

Prodotti


Release

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by