A function related with speed light and time
8 次查看(过去 30 天)
显示 更早的评论
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
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
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
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
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 Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!