matlab help please
% 6.10 This problem requires you to generate temperature-conversion tables. % Use the following equatioins, which describe the relationships between % temperature in degrees Fahrenheit (Tf), degrees celcius (Tc), kelvin % (Tk), and degrees Rankine (Tr), respectively. % Tf = Tr - 459.67 degrees R % Tf = 9/5*Tc + 32 degrees F % Tr = 9/5*Tk % (a) Create a function called F_to_K that converts temperatures in % Fahrenheit to Kelvin. Use your function to generate a conversion table % for values from 0* to 200* F. Tf = 0:200; % Fehrenheit vector from zero to two-hundred c6p10a = FK(Tf); % called c6p10a = Tk, this formula is to use to find kelvins % table displays ouptut of F and K F_K = [Tf; c6p10a]' % (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.
function F_to_K = FK(k) % this function converts degrees Fahrenheit to Kelvin F_to_K = (5/9)*(k+459.67);
The reason I named FK(Tf) c6p10a is because my instructor wants us to use his variable formatting.
@jim_thompson5910
part a @jim_thompson5910
ok let me look it over
okay
does your teacher want you to create a separate m file for the function? or have you put the function inside this current m file?
Formatting and the hw assignment
I don't think he does base on the schedule, 2nd one
`Unless there is a reason not to, begin each problem with the clear and clc commands` so don't forget about that
hmm it makes no mention of functions
okay, I won't. So how does my function look? I can't get it to run, says there is a problem with line c6p10a = Fk(Tf)
Put look at hw 6 on schedule.
Even look at the description of the problem 6.10, posted
you mean this? `Turn in 3 function m-files: F_to_K.m, C_to_F.m,` `& temperature_conversions.m only. No hw5.m` `required. Use HW format reqmnts`
Yes, so I won't turn in 6.10 coding. But I made them for my use.
ok, so you'll need to make the m file `F_to_K.m` and name the function `F_to_K` the function name and the m file name must match up so matlab can find the right function from the right file. If there isn't a match, matlab will let you know with an error
`c6p10a = FK(Tf); % called c6p10a = Tk, this formula is to use to find kelvins` this line is invalid because FK is the wrong function name.
okay let me think about this for a bit.
Yes, got it. Tf = 0:200; % Fehrenheit vector from zero to two-hundred c6p10a = F_to_K(Tf); % called c6p10a = Tk, this formula is to use to find kelvins % table displays ouptut of F and K F_K = [Tf; c6p10a ]'
looks good
okay so F_to_k(Tf) has to match the F_to_K m file? What part needs to match?
F_to_k is the name of the function it MUST match the name of the m file exactly. One character off and matlab won't be able to find the proper file
so if your teacher wants you to call the m file `F_to_k.m` then you must name the function `F_to_k`
Yes, I agree with the statement above. What about under 6.10? I called it homework5 so it's homework5.m
on the line `c6p10a = FK(Tf);` you're calling some function `FK` which matlab will look through the directories (it is told to look through) for the m file `FK.m` but it may not find it. If it does find it, you may be looking in the wrong m file
okay I see what you are saying now. Than answers my question.
did you make a function called homework5?
No, I didn't make a function called homework 5. My understanding is that I need the m file homework5 because that is where I put my vector and the table formula. ALso I need homework5 m file so I can ouput the conversions.
I see
I may email my instructor to double check with him as to whether or not he wants a homework5 m file. There is a possibility he wants just the function m files because he has the other information from each problem.
Basically I had to make homework5 m file to see if my functions work.
I think your prof is looking for only the function files. Since it says `No hw5.m required` but you'll still need your hw5.m file to test out the functions to see if they work or not
Okay I see. Sounds good. Thanks for your help. If I have any additional questions I will tag you.
@jim_thompson5910 subfunctions? I have to re do problem 6.10 but use subfunctionns %% 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.
So these subfunctions would be in the current m files I have for the two separate functions or do I need to make one m file with many function in it?
here's an example of what your teacher wants http://www.mathworks.com/help/matlab/matlab_prog/local-functions.html
Okay I will take a look at it and let me think about this.
see attached. I took a screenshot of that page and I go over the example of where the function and subfunctions are (where they start and end)
@jim_thompson5910
let me think
having a bit of a hard time figuring out the conv_table ? My understanding is that each function should output the conversion factor given Tf?
no, what I think is supposed to happen is that you step 1) go from Fahrenheit to Celsius step 2) go from Celsius to Kelvin then you make a table with the x column being the Fahrenheit and y column being the Kelvin
the subfunctions won't show what is going on. Only the final result is shown. This is to avoid clutter
so what you are saying is only make two sub functions? It asks to make three sub functions?
But I see what you are saying. Basically have the program do the math portion.
Let me think about this some more and figure it out.
hmm I guess Rankine has to be in there too. Tbh, this is the first time I've ever seen Rankine before
Well I solved for Rankine on paper. So I am going to have 3 temp_conv m files for this part of the assignment?
I think so
I found the Rankine formula from the other two formulas
Let me think about this
where is your part b?
part C, he asked us only to do part a and c % (c) Recall that you will need to call your primary function from the % command window or from a script M-file.
ah ok
I will figure this part out later
Can I offer a suggestion? I noticed you wrote F_to_K as one of your variable names. That's a bit clunky. The ouptut of the FK function is the kelvin temperature, so why not just call the output K? Instead of F_to_K
and there's a problem with your temperature_conversions function x is the input x is what exactly? The temperature in Celsius? The temperature in Fahrenheit?
because I am a complicated person lol No, thanks for the suggestion. I will change it to K. Sometimes I am writing this code I just use what is available. Sometimes I have a hard time naming variables because I am trying to organize the info in my head so I just start using what ever variables sounds reasonable.
Yes, I don't know what the x represents in the temperature_conversion(x) I am still trying to understand what x represents relating to the other variables like k, f, c
F_to_K is better suited as a function (or subfunction) name
Take for example this code function [avg, med] = mystats(x) n = length(x); avg = mymean(x,n); med = mymedian(x,n); end function a = mymean(v,n) % MYMEAN Example of a local function. a = sum(v)/n; end function m = mymedian(v,n) % MYMEDIAN Another example of a local function. w = sort(v); if rem(n,2) == 1 m = w((n + 1)/2); else m = (w(n/2) + w(n/2 + 1))/2; end end
x is the input of the `temperature_conversions` function x gets fed into the FK, CR, CF subfunctions so there's a mismatch in units when you have Fahrenheit for the first subfunction but Celsius for the other subfunctions
what's your question about that example?
looking at a = mymean(v,n) this has v and n but no x
it only has v and n in the formula a = sum(v)/n;
so you're wondering why no x?
How does that relate to function [avg, med] = mystats(x)
yes
well x is used but probably not in the way you think n is dependent on x n = length(x); gets the size of x (ie how many numbers we're dealing with)
avg = mymean(x,n); calls the `mymean` function first input is x second input is n go to the definition of `mymean` and we see this line function a = mymean(v,n) so the first input is v, the second input is n match up the inputs v = x n = n
so they could have said function a = mymean(x,n) but chose to go with v instead
okay
Okay so I need 3 temperature_conversions m files each function will have 1 to 2 subfunctions depending on how many equations are involved?
I think you'll have 3 subfunctions based on what you wrote % Tf = Tr - 459.67 degrees R % Tf = 9/5*Tc + 32 degrees F % Tr = 9/5*Tk
^So those are the sub functions? So then these equations are not sub functions % first subfunction FK function a = FK(f) a = (5 / 9) * (f + 459.67); end % second subfunction CR function b = CR(c1) b = (9 / 5) * (c1 + 273.15); end % third subfunction CF function c = CF(c2) c = (9 / 5) * (c2 + 32); end
These are the equations that make the conversion
hmm I'm still having an issue with how you set up the inputs
let me think of how to fix that
kk
I think I may have something. One moment.
ok I had to make two separate m files. I'm not sure how else to work it
why not 3?
here is the first one where the inputs are all in celsius
take note how each subfunction has the same input: temp in Celsius
well except the K_to_R one for some reason I forgot to delete it
brb I need to take care of something
ok
@jim_thompson5910
Tf = 0:200; % Fehrenheit vector from zero to two-hundred c6p15a = temperature_conversions(Tf); % c6p15a = temp_conv which finds % these are the set of subfunctions that will be used to solve for kelvin % given fahrenheit % this function temperature_conversions has the input % of fahrenheit which will given rankine, then input rankine to get kelvin function [R, K] = temperature_conversions(Tf) R = F_to_R(Tf); K = R_to_K(Tf); end % function to solves for rankine given fahrenheit function R = F_to_R(F) R = F + 459.67; end % function solves for kelvin given rankine function K = R_to_K(R) K = (5/9)*R; end
Join our real-time social learning platform and learn together with your friends!