A function related with speed light and time
14 views (last 30 days)
Show older comments
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 Comment
Walter Roberson
on 16 Dec 2022
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)
Accepted Answer
William Rose
on 16 Dec 2022
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 Comments
William Rose
on 16 Dec 2022
@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
on 16 Dec 2022
@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.
More Answers (0)
See Also
Categories
Find more on Lighting, Transparency, and Shading in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!