matlab
On homework 5, problem 6.15 part c is my question
ignore 6.10
Is this part c) % (c) Create a function called C_to_F that converts temperature in Celsius % to Fahrenheit. Use your function to generate a conversion table from 0T* % to 100* C. Choose an appropriate spacing.
% (c) Recall that you will need to call your primary function from the % command window or from a script M-file.
^That is part C
you are looking at 6.10.
%% 6.15 In Problem 6.10 you were asked to create and use three different % temperature conversion functions, based on the following conversion % equations: % Tf = Tr - 459.67 degrees R % Tf = 9/5*Tc + 32 degrees F % Tr = 9/5*Tk % Recreate Problem 6.10 using nested subfunctions. The primary function % should be called temperature_conversions and should include the % subfunctions % F_to_k % C_to_R % C_to_F % Within the primary function use the subfunctions to: clc; clear % clears command and workspace window % (a) Generate a conversion table for values from 0*F to 200*F. Include a % column for temperature in Fahrenheit and Kelvin. Tf = 0:200; % Fehrenheit vector from zero to two-hundred c6p15a1 = temperature_conversions1(Tf); % c6p15a1 = temp_conv1 which finds temp. conversions c6p15a2 = temperature_conversions2(Tf); % c6p15a2 = temp_conv2 which finds tem. conversions c6p15a3 = temperature_conversions3(Tf); conv_table1 = [Tf; c6p15a1]' % table displays fahrenheit in col_1 and kelvin in col_2 conv_table2 = [Tf; c6p15a2]' % table displays fahrenheit in col_1 and rankine in col_2 conv_table3 = [Tf; c6p15a3]' % table displays fahrenheit in col_1 and fahrenheit in col_2 % (c) Recall that you will need to call your primary function from the % command window or from a script M-file.
I don't know much about matlab. Let me check google.
@hartnn
define a file C_to_F.m that contains: % f= function C_to_F(c) % input: temperature in degrees Celcius % returns temp in Fahrenheit function f= C_to_F(c) f= (9/5).*c+32; now you can use this function to create a table. Example: >> cc=0:5:100; % degrees Celcius >> ff= C_to_F(cc); % convert to Fahrenheit >> str=sprintf('%4dº C %4.0fº F\n', [cc; ff]); disp(str); 0º C 32º F 5º C 41º F 10º C 50º F 15º C 59º F 20º C 68º F 25º C 77º F 30º C 86º F 35º C 95º F 40º C 104º F 45º C 113º F 50º C 122º F 55º C 131º F 60º C 140º F 65º C 149º F 70º C 158º F 75º C 167º F 80º C 176º F 85º C 185º F 90º C 194º F 95º C 203º F 100º C 212º F
@phi
Can I just use the C_to_F file i already made for problem 6.10?
% (c) Recall that you will need to call your primary function from the % command window or from a script M-file.
^This is what I need to answer.
does your C_to_F work ?
I would make sure your functions accept vectors of numbers use .* instead of *
my C_to_F works just fine for pobelm 6.10. I am just trying to figure out how to use it for part C of problem 6.15
or do I need to make a new m file. I could call it CC_to_FF
Here is my C_to_F code function C_to_F = CF(f) % this function converts from degrees celsius to degrees fahrenheit C_to_F = (5 / 9) * f - 32; end
your syntax is off (though I think matlab will allow it) I would write it function f = C_to_F( c ) notice I named the input "c" to remind us it is celsius and the output is f (for fahrenheit) and the name of the function is C_to_F and not CF
I see what you are saying, but matlab will accept this function C_to_F = CF(f)
yes, but you really should use C_to_F as the name of the function (and the name of the .m file) and it is confusing to someone who reads "f" as an input. (They will think the input is in degrees fahrenheit)
Oh okay I will change that. Let me think about this a bit more
@phi You have to leave it this way in order for it to run.. function C_to_F = CF(c) % this function converts from degrees celsius to degrees fahrenheit C_to_F = (5 / 9) * c - 32; end
what I tried to do is change it to function CF = C_to_F(c) CF = (5/9)*c-32 end
^But it won't run. the C_to_F = CF(), the C_to_F has to match the m file name, not next to the parenthesis
yes, but you want the file name to match the function name. >> cc=0:5:100; % degrees Celcius >> ff= C_to_F(cc); % convert to Fahrenheit
it is "good form" ... otherwise your code will be confusing to anybody else (and possibly even to you at a later time)
so in this case my file name would be ff
the question is % (c) Create a function called C_to_F that converts temperature in Celsius to me, that means create a file named C_to_F.m and in that file you have function f= C_to_F© stuff....
I see what you are saying, but in matlab it will throw an error if you code it this way. That is what I am trying to say
the f has to be C_to_F in order to run it.
At first I thought the function part was after the equals sign, but while doing this assignment it proved me wrong.
I don't think so. try this: create a file C_to_F.m put in % f= function C_to_F(c) % input: temperature in degrees Celcius % returns temp in Fahrenheit function f= C_to_F(c) f= (9/5).*c+32; then in the matlab command line, clear it, clear; clc then type >> cc=0:5:100; % degrees Celcius >> ff= C_to_F(cc); % convert to Fahrenheit >> str=sprintf('%4dº C %4.0fº F\n', [cc; ff]); disp(str);
give me a sec
let me fix the comment: % f= C_to_F(c) % input: temperature in degrees Celcius % returns temp in Fahrenheit function f= C_to_F(c) f= (9/5).*c+32; end; (It is good to remind the user what the input and output are in the comments at the head of the file. Then if you type >> help C_to_F you will get those comments.
function f= C_to_F(c) ↑ Error: Function definitions are not permitted in this context.
you have to put that in a separate file with nothing else in the file.
give me a sec
So I need to put C_to_F in a separate file with nothing else?
So I made a new file and placed C_to_F in it and ran the code. I get an error.
I thought the file has to be in the same file as my "homework5" in order to run.
what is the error?
Undefined function or variable 'C_to_F'.
this is the problem you had in the past. If you do >> dir do you see your file ?
>> dir . Homework5.asv if else.m ~$govt_camp.xlsx .. Homework5.m temperature_conversions1.m Ex. problems textbook Matlab HW temperature_conversions2.m F_to_K.m Matlab labs temperature_conversions3.m
I had it in this file, but then I removed it like you told me to
the C_to_F.m file should be in the directory.
okay it runs. So why was it running before?
It was probably running incorrectly
I usually put all my functions into their own .m file (one function to a file) I know you can put multiple functions in a file, but rarely do that.
Okay, I am going back to change the variables so it's so confusing.
@jim_thompson5910
@jim_thompson5910
% (c) Recall that you will need to call your primary function from the % command window or from a script M-file. Tc = 0:5:100; % Celsius vector from zero to one-hundred c6p10c = C_to_F(Tc); % outputs fahrenhiet for every celcius input % creates a table with values of celcius in colum_1 and fahrenheit in % colum_2 table_C_F = fprintf('%4.0dº C %4.0fº F\n', [Tc; c6p10c]);
what's your question?
6.15 part c % (c) Recall that you will need to call your primary function from the % command window or from a script M-file.
my solution... Tc = 0:5:100; % Celsius vector from zero to one-hundred c6p10c = C_to_F(Tc); % outputs fahrenhiet for every celcius input % creates a table with values of celcius in colum_1 and fahrenheit in % colum_2 table_C_F = fprintf('%4.0dº C %4.0fº F\n', [Tc; c6p10c]);
how is the table turning out?
º C -32º F 5º C -29º F 10º C -26º F 15º C -24º F 20º C -21º F 25º C -18º F 30º C -15º F 35º C -13º F 40º C -10º F 45º C -7º F 50º C -4º F 55º C -1º F 60º C 1º F 65º C 4º F 70º C 7º F 75º C 10º F 80º C 12º F 85º C 15º F 90º C 18º F 95º C 21º F 100º C 24º F >>
strange. 100 C is not equal to 24 F
0 C = 32 F 100 C = 212 F
that isn't good because my C_to_F function for problem 6.10 spits out 23.5556 for 100* C
you're using F = (9/5)*C + 32 right?
or F = 1.8*C + 32 which is equivalent just in decimal form instead of fraction form
no i am using F = (5/9)*c-32
you're mixing that up with C = (5/9)*(F - 32)
100 => 212
F = (9/5)*c+32;
so for 100* C I get 212* F
much better
so since my instructor wants to only see the function m files how am I suppose to show him that I did part c of problem 6.15 ?
He doesn't want the homework5.m file which is strange because that m file has a huge chunk of my hw.
can i see the pdf where your teacher gives the instructions?
yeah give me a sec
Formatting too. I am a little confused if I need clear, clc in function m files or not. I emailed him about formatting because I am still a bit confused. He hasn't responded. The homework is due at 10pm .
on your hw formatting requirements it says `Unless there is a reason not to, begin each problem with the clear and clc commands` so I'd have clc and clear
on each function m file?
you do not need clc or clear in the function files. The top of the function file should be the line setting up the main function `function [output1,output2,...,outputN] = name(input1,input2,...,inputN)`
the reason why you don't need `clc` in a function file is because the function should not show anything to the command window. That's the job of the m file that calls the function You don't need `clear` either because each variable in the function is a local variable that is only accessible to the function. The only thing that the m file (that calls the function) can touch is the output variables of the function. But those values will be cleared by the m file
okay, good to know.
I think that is all the questions I have now and will be submitting the assignment.
btw, each time the function starts, the local variables are set up in memory. At first, just blank slots then the local variables get values in them when the function ends, the output variables return to the script function. This is when the function will delete and deallocate the local variables in memory
so in a way, the function is using `clear` so another reason why you don't need it
btw i can't get %( blah blah blah %) to work
http://www.mathworks.com/matlabcentral/answers/122-how-do-i-comment-out-a-large-block-of-code-in-matlab someone is saying `% {` `commented stuff` `%}`
hmmm interesting
you can also select the text and do a block comment
% { commented stuff %} it doesn't work.. How do I block comment? Is there a button in matlab?
should be in the edit dropdown menu
your version is different from mine
I don't see it. I am not too worried about it. So what about part c on problem 6.15. I am going to basically have to turn in homework5 to show my instructor I did problem 6.15, part C?
you probably have a version older than version R14
if your teacher hasn't gotten back to you, I would turn in hw5.m just in case. I know it says not to, but it seems odd how you do all that work but not turn it in
Join our real-time social learning platform and learn together with your friends!