rounding function for converting it to an integer value.

2 次查看(过去 30 天)
I am a beginner to MATLAB. I am trying to solve one of the problems from the book I am practicing MATLAB and the problem is
Imagine that you are the owner of a car rental company with two locations, Albany and Boston. Some of your customers
do “one-way rentals,picking up a car in Albany and returning it in Boston, or the other way around. Over time, you
have observed that each week 5% of the cars in Albany are dropped off in Boston, and 3% of the cars in Boston get
dropped off in Albany. At the beginning of the year, there are 150 cars at each location.
Write a script called car update that updates the number of cars in each location from one week to the next. The
precondition is that the variables a and b contain the number of cars in each location at the beginning of the week.
The postcondition is that a and b have been modified to reflect the number of cars that moved.
To test your program, initialize a and b at the prompt and then execute the script. The script should display the
updated values of a and b, but not any intermediate variables.
Note: cars are countable things, so a and b should always be integer values. You might want to use the round function
to compute the number of cars that move during each week.
If you execute your script repeatedly, you can simulate the passage of time from week to week. What do you think will
happen to the number of cars? Will all the cars end up in one place? Will the number of cars reach an equilibrium, or
will it oscillate from week to week?
I wrote a code for the problem.
% companies at two places Albany and Boton are represented as A and B%
j1=150;
j2=150;
i=1;
u(i)=j1-(5/100)*j1+(3/100)*j2;
j1=round(u(i)); %From A to B
u(i)=j2-(3/100)*j2+(5/100)*j1; %from B to A
j2=round(u(i));
disp('week Car at A Car at B')
fprintf('%.0f\t\t %.0f\t\t %.0f\t\t \n',i,j1,j2)
for i=2:30
u(i)=j1-(5/100)*j1+(3/100)*j2;
j1=round(u(i)); %From A to B
u(i)=j2-(3/100)*j2+(5/100)*round(u(i-1)); %from B to A
j2=round(u(i));
fprintf('%.0f\t\t %.0f\t\t %.f\t\t\n',i,j1,j2)
if j1==0
break;
end
end
But the problem I get is for the first three results,they are okay as the total number of cars after update is conserved. But may be because of the rounding process,the total number of cars is not equal to 300 (i.e 150+150). I don't understand where have I gone wrong or how do I solve it. Anyway who could suggest me a correct way to solve the problem? And also help me with the last question,what will happen to the number of cars?

采纳的回答

Guillaume
Guillaume 2017-10-6
编辑:Guillaume 2017-10-6
My first advice would be to use much better variable names. Use full words that explain what is in the variable. Right now, you know in your head what j1, u1, etc. are. For you, in 6 months time, and for anybody else, now, it's completely meaningless and make it that much harder to follow your code.
Secondly, I don't really understand why you've separated the i=1 case, from the i=2:30. They're doing the exact same thing so why not have everything in the loop. Good program design avoids repeating anything. If it's repeated it needs to be a function or a loop.
To avoid the issue with total car count changing, what you need to round is not the number of cars at each location, but the number of cars that are transferred. Obviously, if n cars leave location A, n cars arrive at location B, so here is how I'd code it:
AlbanyCars = 150;
BostonCars = 150;
disp('week Car at A Car at B');
fprintf('%d\t%d\t%d\n', 0, AlbanyCars, BostonCars);
for week = 1:30
CarsFromAlbany = round(AlbanyCars * 0.05);
CarsFromBoston = round(BostonCars * 0.03);
AlbanyCars = AlbanyCars - CarsFromAlbany + CarsFromBoston;
BostonCars = BostonCars - CarsFromBoston + CarsFromAlbany;
fprintf('%d\t%d\t%d\n', week, AlbanyCars, BostonCars);
end
The output of this code should also help you answer the last question.
  1 个评论
reema shrestha
reema shrestha 2017-10-6
I was just trying on my own so I am sorry that I forgot to write the comment. I divided the initializing part i=1 and i=2:30 for some reason I thought would be a right way but I was wrong. Thank you so much. Your code worked perfectly fine. I found where I was wrong.

请先登录,再进行评论。

更多回答(0 个)

标签

Community Treasure Hunt

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

Start Hunting!

Translated by