Writing a MATLAB script for equations

조회 수: 17 (최근 30일)
Fergal Ahern
Fergal Ahern 2019년 5월 14일
댓글: James Tursa 2019년 5월 15일
How would I write a mathlab script to convert failure rate ( lambda) into MTBF using the formula MTBF = 1/LAMBDA. I know Lambda must be assigned a value but not sure how I would write the script.
Any help at all would be much appreciated, thank you

채택된 답변

James Tursa
James Tursa 2019년 5월 14일
편집: James Tursa 2019년 5월 14일
E.g., put these lines in a file with a .m extension:
lambda = input('Input a value for lambda: ');
mtbf = 1 ./ lambda;
Are you supposed to specifically write a script file, or a function file?
  댓글 수: 2
Fergal Ahern
Fergal Ahern 2019년 5월 15일
Thank you for your answer.
Im new to MATLAB, for this example it is to write a script file but what would be the difference between a script and function file?
James Tursa
James Tursa 2019년 5월 15일
If you had those two lines above in a file called myscript.m, and did this at the command line:
>> myscript
Then those lines would be executed as if you typed them in manually at the command line and all variables created by the script would remain in your workspace.
A function file, on the other hand, operates in its own separate workspace. It may take inputs and provide outputs. E.g., suppose you had a file called myfunction.m with the following code
function mtbf = myfunction(lambda)
if( nargin == 0 )
lambda = input('Input a value for lambda: ');
end
mtbf = 1 ./ lambda;
return
end
Then myfunction would take an input (called lambda in the function workspace), calculate mtbf, and return mtbf to the caller. If the user called myfunction without an input, then the function would prompt the user for the lambda to use. The variables in the caller workspace do not have to be named lambda and mtbf ... they can be any valid name the user wants.
Sounds like you will be getting to functions later on ...

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Install Products에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by