For loop and if statement in MATLAB

Please, I need a quick help with this problem. If you can guide me on how I can use a "for loop" and "if statement " to solve the problem I would really really appreciate it. Even if you just try to let me know how to use 'if and for' in anything similar. Thank you

8 comentarios

Adam
Adam el 10 de Abr. de 2018
Have you tried the obvious
doc if
doc for
to look at the help?!
Guillaume
Guillaume el 10 de Abr. de 2018
The question, apart from the typos, is actually very well written since it starts by telling you to solve the most crucial step first:
Create a flowchart
Have you done that yet? Once you've done that the actual usage of if and for is trivially solved using the doc as Adam says.
Note that you won't get much pity from us. The whole purpose of homework and assignments is to prove that you have the skill to solve such problems yourself, not the skill to go and ask somebody else to do it.
Samantha Cepeda
Samantha Cepeda el 10 de Abr. de 2018
yes, yes, you're right and I want to learn, I'm not here to get this problem solved. It's not my homework, I need to know how to solve use Matlab for thermofluids so I'm just taking some problems a Matlab class did to learn the program. I tried to search for solved problems but I got a super complex problem that I don't even understand what it is asking. I'm new to all this. So when you said doc you mean in the same Matlab search for help? and no I haven't done flowcharts before. Does Matlab do flowcharts? if you know of any material besides de same MATLAB help search tab, please let me know as well. Thank you both
David Fletcher
David Fletcher el 10 de Abr. de 2018
If it's not homework, I wouldn't worry too much about doing a formal flowchart. Just make a list of things that you need to do and calculate in some sort of chronological order and take it from there.
Jim Riggs
Jim Riggs el 10 de Abr. de 2018
Editada: Jim Riggs el 10 de Abr. de 2018
I'll give you another hint.
Since you have an orderly sequence of conditions to evaluate, this would be the FOR loop (0.0, 0.1, 0.2, ... etc). These are fractions of m. (The problem statement even indicates "step=0.1m") But you know that if you get to 1.0m you will have gone too far, so inside the FOR loop, use the IF statement to determine when you have gone far enough and exit the FOR loop.
Samantha Cepeda
Samantha Cepeda el 11 de Abr. de 2018
thank you Jim, this is the help I was needing. Thanks I'm trying it out right now. I just noticed that the mass of the whole doesnt even get to 65%. :) I'll post back what I got to see if I made it. Thanks
Adam
Adam el 12 de Abr. de 2018
doc
is just what you can type on command line, followed by whatever you want, to open the help for that particular function. You can equally well open the help yourself and type 'for' or 'if' in the search field, it's just easier to type
doc for
and go straight there. This is always better than using the online help too (unless you always use the most up to date version of Matlab in which case they are equally good) as the online help is always for the latest version. For something as basic as 'for' or 'if' I doubt the help changes much between versions, but some functions you find online may not exist for you if you are using an older version of the software.
got it! thank you. I ended up doing this but I'm not sure how to print the Wr(i) like the current value for the side of my square.
clear, clc
rc = input ('Enter the radius of the cylinder in meters: '); lc = input ('Enter the length of the cylinder in meters: '); wo = input ('Enter the initial width of the hole in meters: '); ws = input ('Enter the step size of the hole in meters: ');
rho = 2700; %kg/m^3 wr = wo:ws:rc;
if wo < rc
for i = 1:length(wr)
mass_cyl = (pi * (rc)^2 * lc)/ rho;
mass_hole = ((wr).^2 * lc)/ rho;
tmass_cyl = mass_cyl - mass_hole;
end
end
fprintf ( '\nThe radius and length are %0.1f m and %0.1f m respectively.\nThe density of the cylinder material is %0.3f kg/m^3.\nThe rectangular hole width is %0.3f m.\nThe total mass remaining is %0.5f kg.\n\n', rc, lc, rho, wr(i), tmass_cyl)

Iniciar sesión para comentar.

Respuestas (2)

Jim Riggs
Jim Riggs el 13 de Abr. de 2018
Editada: Jim Riggs el 13 de Abr. de 2018
First of all, when calculating the masses you have to MULTIPLY the volume by the density.
You should always verify this using dimensional analysis. For example: the calculation pi*rc^2*lc is the volume of the cylinder and has units of (m^3) if you divide m^3 by kg/m^3, that's the same as multiplying m^3 by m^3/kg and the units you end up with are m^6/kg. Now if you multiply m^3 by Kg/m^3 you end up with Kg, which is the correct unit for mass. so,
volume_cyl = (pi * rc^2 * lc); % cylinder volume in m^3
mass_cyl = (pi * rc^2 * lc)* rho; % cylinder mass in Kg
% (or)
mass_cyl = volume_cyl * rho; % Cylinder mass in Kg
Now, this cylinder volume and mass represents the cylinder without the hole, so it does not change, and therefore, this calculation can be placed outside the FOR loop.
The FOR loop is intended to vary hole size, so the loop index 'i' must be used to define the size of the hole, in this case it is the length of one side of the square hole. You are instructed to make the step size 0.1m, so this can be built into the loop. But, rather than use "i" as the loop index, you might just as well use the variable name for the hole width, "Wr". This gives you
for Wr = 0:.1:1 % start at 0, step by 0.1 up to 1
Inside the for loop, you calculate the mass of the hole and subtract it from the mass of the cylinder
mass_cyl = (pi * rc^2 * lc)* rho; % cylinder mass (Kg)
% put a print statement before the loop to show the problem variables that don't change
fprintf('lc=%f, rho=%f, mass_cyl=%f ...etc \n ',lc, rho, mass_cyl, ...etc)
for Wr = W0:Ws:rc % Wr starts at W0, steps by Ws up to rc
mass_hole = Wr^2*lc*rho; % mass of hole (Kg)
tmass_cyl = mass_cyl - mass_hole % mass of the cylinder with the hole in it (Kg)
% put a print statement inside the loop to see each loop calculation
fprintf('Wr=%f, mass_hole=%f, tmass_cyl=%f \n ',Wr, mass_hole, tmass_cyl)
end

11 comentarios

Samantha Cepeda
Samantha Cepeda el 13 de Abr. de 2018
Thank you! you have no idea how much this helps and there's a lot of information I didn't know regarding 'fprintf' in your comment. Yes, I made a mistake from the very beginning. I understood what you did with the wr = 0:0.1:1, but in the case that the user will input the initial and step values, can I still use the variables in my 'for index=values'?
Jim Riggs
Jim Riggs el 13 de Abr. de 2018
Editada: Jim Riggs el 13 de Abr. de 2018
I just noticed that you made an error in the unit conversion of the density from g/cm^3 to Kg/m^3.
It goes like this:
2.7 g/cm^3 x (1 kg)/(1000g) x [(10cm /(1m)]^3
= 2.7 g/cm^3 x 1/1000 (g to Kg) x 1000/1 (per cm^3 to per m^3) = 2.7 Kg/m^3
so 2.7 g.cm^3 = 2.7 Kg/m^3
Samantha Cepeda
Samantha Cepeda el 13 de Abr. de 2018
Oh no! never mind I can see that you did that. your solution is pretty understandable and clear. I know there's never one and only one answer to this questions so i was trying to incorporate the for i=length(wr), to later use it in the formula and write the mass in terms of i and rc in terms of i, like a vector or an array (I'm not sure what they are called)? ie. mass(i) = 2 * (rc(i)).^2 something like that. Should this be possible? I have one more question if you don't mind. if I want to use the 'while' instead of 'for' I know they don't do the same thing, what would be a possible solution for it. I tried and the program never stops I needed to close matlab I thought probably they were a lot of outputs but I read it's because I have an indefinited expression.
Samantha Cepeda
Samantha Cepeda el 13 de Abr. de 2018
hmm... no I think I'm right with that conversion 2.7 g/cm^3 is 2700kg/m^3 1kg = 1000g and 1cm^3=(0.001)^3m^3
Jim Riggs
Jim Riggs el 13 de Abr. de 2018
Editada: Jim Riggs el 13 de Abr. de 2018
you can use a while loop, but to do that you have to include the conditional calculation inside the loop. For example
Wr = W0;
While Wr <= rc
...
...
Wr = Wr + Ws;
end
You see, you have to initialize Wr before the loop, then you have to change Wr inside the loop, or the loop will never end. This loop now will end when Wr is greater than rc.
As you can see, this is all accomplished in a single line in the FOR statement.
Jim Riggs
Jim Riggs el 13 de Abr. de 2018
Also, if you do get stuck in an infinite loop, just press ctrl + C and it will stop execution.
Jim Riggs
Jim Riggs el 13 de Abr. de 2018
Editada: Jim Riggs el 13 de Abr. de 2018
No, you are wrong with the unit conversion:
2.7g = .0027Kg
1 cm^3 = .001m^3
.0027kg/.001m^3 = 2.7Kg.m^3
2.7 g/cm^3 x 1000 (cm^3/m^3) x .001 (Kg/g) = 2.7 Kg/m^3
Samantha Cepeda
Samantha Cepeda el 13 de Abr. de 2018
but how can 2.7 g/m^3 be the same as 2.7 KG/m^3 if the amount first is in grams and than kg.
Jim Riggs
Jim Riggs el 13 de Abr. de 2018
Editada: Jim Riggs el 13 de Abr. de 2018
You are right. there are 100 cm/m
2.7 g/cm^3 x (100)^3 x .001 = 2700 Kg/m^3.
I apologize.
No, you're good. When I tried to use the fprintf inside the for loop it doesn't print all the values but the last one. I ended up using the i symbol that I'm still unsure if I'm using it right and I got the same last mass as your code gives me but I see that the wr varies sooo much and if I use numbers from rc=3,lc=5,wo=1 and ws=0.5 I should get wr= 1, 1.5,2,2.5,3 nothing else but I got 54000.00. DO you have an idea what's going on there? here's my code:
hmm.. when it asks for the current value, it means the last value right? or all the current values? this task is somehow not well written from my perspective.
clear, clc
rc = input ('Enter the cylinder radius: ')
lc = input ('Enter the cylinder length: ')
wo = input ('Enter the initial width of the hole: ')
ws = input ('Enter the increment of the hole size: ')
rho = 2700
wr = wo:ws:rc
mass_c = pi * (rc)^2*lc*rho
if wo < rc
for i = 1:length(wr)
mass_h(i) = (wr(i)).^2 *lc*rho
tmass = mass_c - mass_h
fprintf(' wr=%f, masshole=%f, totalmass=%f \n', wr(i),mass_h,tmass )
end
end
Samantha Cepeda
Samantha Cepeda el 14 de Abr. de 2018
I put it betwen the two last 'end' and it worked well. I'm just unsure if the i that I'm using because I couldn't print all your the values out in your code, it's correctly used. Also I read that using array I need to use the .^ or .* or ./. the control + C did work out! thanks haha I was going crazy! I'm gonna do a while loop now I think I undertand how the for loop works using an if statement now. it's just the operators (symbols) and know how and where to place f printf.

Iniciar sesión para comentar.

Samantha Cepeda
Samantha Cepeda el 14 de Abr. de 2018
No, you're good. When I tried to use the fprintf inside the for loop it doesn't print all the values but the last one. I ended up using the i symbol that I'm still unsure if I'm using it right and I got the same last mass as your code gives me but I see that the wr varies sooo much and if I use numbers from rc=3,lc=5,wo=1 and ws=0.5 I should get wr= 1, 1.5,2,2.5,3 nothing else but I got 54000.00. DO you have an idea what's going on there? here's my code:
clear, clc
rc = input ('Enter the cylinder radius: ')
lc = input ('Enter the cylinder length: ')
wo = input ('Enter the initial width of the hole: ')
ws = input ('Enter the increment of the hole size: ')
rho = 2700
wr = wo:ws:rc
mass_c = pi * (rc)^2*lc*rho
if wo < rc
for i = 1:length(wr)
mass_h(i) = (wr(i)).^2 *lc*rho
tmass = mass_c - mass_h
fprintf(' wr=%f, masshole=%f, totalmass=%f \n', wr(i),mass_h,tmass )
end
end

2 comentarios

Jim Riggs
Jim Riggs el 14 de Abr. de 2018
The first thing that I notice is that in your print statement you are printing "mass_h", but this is a vector. This means that each time the print statement is encountered, it is printing all of the mass_h values. What you want is "mass_h(i)".
Jim Riggs
Jim Riggs el 14 de Abr. de 2018
Editada: Jim Riggs el 14 de Abr. de 2018
One other observation: The statement (wr(i)).^2 is a matrix statement that indicates each element in the matrix is to be squared (that's what the dot "." is for). However, using the subscript (i) means it is only referencing one element (the ith element) so this is a scalar. This means that you don't need the "." and can simply write wr(i)^2 as this is performing a scalar operation.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 10 de Abr. de 2018

Editada:

el 14 de Abr. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by