Help with loops and plotting with functions

I have this assignment, and I'm stuck on making this function. This is what we were given:
This function has a finElems parameter and doesn't return anything. It plots all the temperature values found in the finElems array. You can see my example plot below. To plot a color, use a statement like this:
plot(colNum, numRows-rowNum, 's', 'Color', color,... 'MarkerFaceColor', color, 'MarkerSize', 20);
In order to use this statement you should create a few variables:
• colNum: This is the current column number. You will iterate through all the columns in the array, so store the current column number in this variable.
• rowNum: Same as colNum, except it's the current row number.
• color: This is the color of the current finElems element as determined by the function tempToColor.
I found it necessary to use the expression numRows - rowNum so that the bottom row of the array (the highest numbered row) appears as the bottom row on the graph. Otherwise the fin appears upside down in the graph. Use this outline for the function:
function plotFin(finElems)
hold on;
% iterate through all the rows:
% iterate through all the columns: (this is a loop within a loop)
% Use the tempToColor function to get the temperature.
% Plot the temperature color.
% end
% end
axis equal tight;
axis([0 (numCols+1) -1 numRows]);
hold off;
end
So far, what I have for the plotFin function is:
function plotFin(finElems)
hold on;
for rowNum=1:10
for colNum=1:20
tempToFin(temp)
plot(colNum, numRows-rowNum, 's', 'Color', color,...
'MarkerFaceColor', color, 'MarkerSize',20);
end
end
axis equals tight;
axis([0 (numCols+1) -1 numRows]);
hold off
end
And what I have for my tempToColor function is:
function color = tempToColor(temp)
persistent map
if isempty(map)
map = jet(steamTemp-coolantTemp+1);
end
color=map(temp+1,:)
end
I'm stuck mainly on trying to use tempToColor to get the temperature to plot it. Thanks in advance for your help!! :D

 Respuesta aceptada

Adam
Adam el 13 de Nov. de 2014

0 votos

What exactly are you stuck on?
For a start though you are not assigning the output of tempToColor to a variable so your 'color' variable in plotFin will be unassigned.
Is that your only problem or are there others?

7 comentarios

Andrew Ardolino
Andrew Ardolino el 13 de Nov. de 2014
I'm not sure how to put color into plotFin, and then I'm not sure how to use tempToColor to plot the colors based on the temperature, I guess
Adam
Adam el 13 de Nov. de 2014
Well, putting color into the plot function will simply be a case of doing what I said above and assigning the result of tempToColor to the variable 'color'. You are already passing that variable to the plot function.
In terms of your tempToColor function itself...
where are steamTemp and coolantTemp defined? I assume these are the min and max limits for your temperatures, but they need to be defined somewhere. Either hard code them in your function if they don't change or pass them in as arguments to the function if they can change.
Once you have done that I don't think that is how I would go about mapping your values onto the colourmap though. If coolantTemp and steamTemp are both integers within a sensible range of each other (e.g. no more than ~500) then I guess defining the colourmap that way is ok, but you need to map your temp onto that scale which your maths will only do if coolantTemp is 0.
Andrew Ardolino
Andrew Ardolino el 14 de Nov. de 2014
steamTemp and coolantTemp are two other functions. I'm still confused about how to put color and tempToColor into that function though
I tried doing this but it doesn't work :( not sure where else to go from here.
function plotFin(finElems)
hold on;
color=tempToColor(temp)
for rowNum=1:10
for colNum=1:20
tempToFin(temp)
plot(colNum, numRows-rowNum, 's', 'Color', color,...
'MarkerFaceColor', color, 'MarkerSize',20);
end
end
axis equals tight;
axis([0 (numCols+1) -1 numRows]);
hold off
end
Adam
Adam el 14 de Nov. de 2014
Editada: Adam el 14 de Nov. de 2014
Ah, I misread your tempToFin function call as being tempToColor.
You need to put that
color = tempToColor line inside the nested for loops I assume as that is where temp changes, though your code doesn't include any definition of the variable temp either. I assume you need a line something like:
temp = finElems( rowNum, colNum );
inside your nested for loops as well.
Andrew Ardolino
Andrew Ardolino el 14 de Nov. de 2014
okay, now I get errors when I try to run that, but I'm working on it. Do you think you'd be able to help me with the next function also? My teacher doesn't email us back and the homework he gives us are way past what he teaches us in class.
Next function should be:
6.8. Function placeAirTemp
This function has one parameter: the fin element array, and it returns a fin element array. In this function you must place the air temperature values along the top edge (row 1) of the array, and down the left and right edges of the array (the right edge is column numCols). Use the value from the airTemp function.
I have no idea where to even begin with this one:P
Andrew Ardolino
Andrew Ardolino el 14 de Nov. de 2014
I know I'd need to do something to make it go from 1:numRows and 1:numCols But I'm not sure how to go about doing that.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Etiquetas

Preguntada:

el 13 de Nov. de 2014

Comentada:

el 14 de Nov. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by