I don't know how to make and algorithm to find BMI?

Answers (1)

This looks like homework.
Try creating a function. Inputs will be height and weight, and output will be the BMI.
Some useful links that might help you:

10 Comments

It is unfortunately is, im supposed to write a MATLAB program that calculates BMI for any given patient when height and weight are entered, but I don't know what to do, im just really struggling in class, with all my confusion.
It's difficult to help you and not do it all for you. If you don't know Matlab at all, try looking on YouTube, asking your classmates or your teacher. If you already know a little bit of Matlab, check out the link that describes the functions in Matlab (which I already gave you here).
I can help you and explain a bit.
This a function that I created. It's called myFunction, that will take input and add 5 to it.
function [result] = myFunction(input)
result = input + 5
end
Now, you can use this function and insert ynything appropriate that the function can handle. In this case, it's numbers or vectors.
If I would type this into the command window:
myFunction(5)
the result would be 10. (5 + 5 = 10)
You can have more results and more inputs. Functions name and variable names are up to you.
Your function will very likely look like this, let's name it "BMI".
function [bmi] = BMI(weight, height)
bmi = ... % your calculations here
end
Then, you may call this function like so:
BMI(100, 180)
and it will give you a BMI result, which will depend on the things you used to calculate it.
Hope I helped.
@Brandon - Take the great advice of @Vilém Frynta. But start writing. If you make some progress, then you can ask again, and show what you did, as well as explain where you ar getting stuck. But you need to start writing, as that is how you will learn.
If all of this really is just too confusing, then you need to start out with a tutorial about MATLAB. The MATLAB Onramp is a good place to start.
I think my instructor wants me to do it in a script, but class ended before she could even cover that part on Friday, and campus has been closed the entire week after, due to a winter storm in my location. I don't know the diffrence between "script" and "live script". Is a function and a script the same thing?. I am also prompted to imput the name of the patient, how do I do that, how do I imput a name?.
Script can be a function. You need to create function (as I showed you above) and save the script under the function name. So if you have function called myFunction, you need to save the script as myFunction.m. You can call this function in your other scripts as well.
Patient's name can be part of your function.
function [bmi] = BMI(weight, height, patientname)
bmi = ... % your calculations here
end
You didn't even mention whether you are processing a Excel spreadsheet or something, but ... nevermind. I think we helped you a lot but now you need to try. Let us know your progress.
Read the documentation section on Function Basics.
I copied and pasted the function you made called "myFunction" into the script, but when I put "myFunction(5)" in the command window it just keeps telling me "Unrecognized function or variable 'myFunction'." and I don't know why, I've been checking if i've been using caps and lower case when spelling or copying and pasting, but it keeps telling me the same thing. What am I doing wrong?
Brandon, if you show the script, then we are able to debug the code and identify the faults.
make sure you put the function code into myFunction.m
Also, make sure you did not write the function inside one of MATLAB's installation directories. MATLAB assumes that any program or person that modifies the installation directories will specifically tell MATLAB to check for new files. This is unlike if your current directory is one of your own directory and you use the MATLAB editor to modify (or create) the file: in that case, MATLAB will notice the change and permit the function to be called.
If you're putting the function into a script, then you won't be able to call from the command line. It's local to the script, so you call it within the script.
% this is a script
% call the local function
result = myfunction(5)
result = 50
% this is a local function within a script
function out = myfunction(in)
out = in*10;
end
Otherwise, you can create a function file and place it somewhere on the path. Then you can call it where ever you want. See Star Strider's link.
Note that while you can turn any script into a function, function files contain only functions, and are treated differently than script files. Unless you've structured the file as a function, you'll have issues trying to execute it as a function.

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Asked:

on 3 Feb 2023

Edited:

DGM
on 5 Feb 2023

Community Treasure Hunt

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

Start Hunting!