should i us if/eslse if or int.

조회 수: 1 (최근 30일)
Danielle deWaard
Danielle deWaard 2021년 7월 29일
답변: Kshitij Chhabra 2021년 8월 4일
I am having trouble with a code that is depicting days. i have three different rates. one being the daily rate used for 1-2.4 days. weekly rate starts from 2.5-17.63. and monthly goes from 17.65-30 days.
At first thought, the If/Elseif seems the most logical until it becomes something like 34 days, where i need the program to depictthat it is 34 days and now it needs to add the monthly and the weekly or 61 days - where it now needs to depict two monthly rates and then a daily rate. and so on and so forth
I dont know if someone has any advice or more knowledge on the intergration function and if that is a route i should consider taking instead.
Thank you in advance for any help.
  댓글 수: 1
Cris LaPierre
Cris LaPierre 2021년 7월 29일
I understand the issue with number of days, and how an if/else statement can possible help out there. You lost me, though, when you started talking about integration. I think we are missing some important details necessary for understanding your question. Can you please clarify? If you have some code already, please share that as well.

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

답변 (1개)

Kshitij Chhabra
Kshitij Chhabra 2021년 8월 4일
From my understanding of the question, you want to implement an if/else if ladder to calculate the rate which you should use for the following day. An approach which I can suggest is using the mod function along with division in MATLAB.
In your case, lets say that you find the rate for 68 days and you have a dailyRate, weeeklyRate and a monthlyRate, you can compute the final rate as follows:
input=68;
months=floor(input/30);
days=mod(input,30) ;
rate=0;
if(days<=2.4)
rate=dailyRate;
elseif(days>=2.5 & days<=17.63)
rate=weeklyRate;
else
rate=monthlyRate;
end
rate=monthlyRate*months+rate;

카테고리

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

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by