I am a single function script called ‘TemperatureConvert.m’ that inputs some temperature in Celsius and outputs the temperature in Celcius, Kelvin, and Fahrenheit.

조회 수: 5 (최근 30일)
Knowing °K = °C + 273, and °F = ((9/5)* °C) + 32.
I created the first script defining a function:
function [kelvin, farenheit] = temperatureconvert(celcius)
kelvin = celcius + 273;
farenheit = (9./5).*(celcius + 2);
end
I then made a second script to call the function where I was told to calculate the converted temperature of the degrees 0 to 100 with an interval of 5 degree celcius. As shown below.
choice = input('input value for x')
for choice = 1:5:100
temp = temperatures(choice);
disp(temp)
end
I am getting values, but I know they are not right because I need to be getting outputs of both kelvin and farenheit. What should I change in order to get both these values for this set of celcius inputs, and why? I am fairly new to MATLAB and fairly understand the general functions and uses, but I am having trouble peicing them all together.

채택된 답변

Cris LaPierre
Cris LaPierre 2018년 12월 4일
편집: Cris LaPierre 2018년 12월 4일
You say you are to convert temps from 0 to 100 with interval of 5 degrees, but then set up your for loop using 1:5:100. You are not converting 0, 5, 10, ... but instead 1, 6, 11, ...
Also, you have set up your function to return two variables
function [kelvin, farenheit] = temperatureconvert(celcius)
but are only assigning its output to one.
temp = temperatures(choice);
If you want to capture both outputs from the function you have to assign the output to two variables
[tempK, tempF] = temperatures(choice);
or have the function return a vector containing both values
function out = temperatureconvert(celcius)
kelvin = celcius + 273;
farenheit = (9./5).*(celcius + 2);
out = [kelvin, farenheit];
end
Also note that your function is called temperatureconvert but when you call it, you are using the name temperatures. These should be the same, and should be whatever name you used in the function declaration line (temperatureconvert here).

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Power and Energy Systems에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by