A function related with speed light and time

조회 수: 7 (최근 30일)
Lewis HC
Lewis HC 2022년 12월 15일
편집: Lewis HC 2022년 12월 22일
Hello dear friends, I need to write a function called Ltime that takes as input a row vector of distances in miles and returns two output arguments. The first output argument is a row vector of the corresponding time in minutes for light to travel each distance, it takes light a little over 8 minutes to travel from the sun to the earth which are 92.9 million miles apart. The second output argument is a vector of the inputs in kilometers. The speed of light is 300,000 km/s and a mile is 1,609 km.
Also I should this code for getting the function: [a,b]=Ltime([10,100,1000,10000]). Thank you!
  댓글 수: 1
Walter Roberson
Walter Roberson 2022년 12월 16일
Your code does not define A, B, C or D before the second line is executed . Are they functions that take no arguments? And then you overwrite the input M , and then you overwrite the four functions??
I am having trouble figuring out what anything in your function has to do with the assignment, with the exception that V=300000 looks like an approximation for the speed of light (which would normally be coded as c instead of V)

댓글을 달려면 로그인하십시오.

채택된 답변

William Rose
William Rose 2022년 12월 16일
It is nice to see that you have made an attempt before posting your question.
@Walter Roberson is exactly right as usual.
You had the right idea when you wrote "[a,b]=Ltime([10,100,1000,10000])". Use a syntax like that in the funciton declaration (line 1).
Short version
function [a,b]=Ltime(M)
a=M*8.947e-8;
b=M*1.609;
end
Longer version
function [a,b]=Ltime(M)
%LTIME returns light time in minutes, and distance in km.
%Input
% M=vector of distances in miles
%Outputs
% a=time in minutes for light to travel the distances in M
% b=distances M, converted to km
c=2.9979e5; %speed of light in km/s
b=M*1.6093; %convert miles to km
a=b/(c*60); %time in minutes
end
Note that 1.6093/(c*60)=8.948e-8, the constant in the short version of the function.
  댓글 수: 4
William Rose
William Rose 2022년 12월 16일
@Lewis HC, you're welcome.
"there are small problems that confuse me sometimes"
I often feel the same way, and I bet most Matlab users do.
William Rose
William Rose 2022년 12월 16일
@Walter Roberson, thank you for the feedback. Good point. I guess I was more helpful than I should have been because he showed his own code and his other idea. Which many people don't do.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by