フィルターのクリア

Write a function called fare that computes the bus fare one must pay in a given city based on the distance travelled. Here is how the fare is calculated: the first mile is $2. Each additional mile up to a total trip distance of 10 miles is 25 cents.

9 ビュー (過去 30 日間)
function a = fare(dist,age)
x=round(dist);
if x>=0 && x<=1
fare=2;
if x>1 && x<=10
fare=2+(0.25*(x-1));
if x>10
fare=4.25+(0.1*(x-10));
end
end
end
if age<=18 || age>=60
a=0.8*fare;
else
a=fare;
end
end
  1 件のコメント
Sudarshan Agrawal
Sudarshan Agrawal 2018 年 6 月 16 日
i am getting the following error:- Undefined function or variable 'fare'.
Error in fare (line 15) a=fare;

サインインしてコメントする。

回答 (4 件)

dpb
dpb 2018 年 6 月 16 日
function a = fare(dist,age) ... fare=2; ...
Unlike Fortran, the return value in a Matlab function is not stored in the function as a variable of the function name but one uses the defined return variable instead. You defined that as a in the function declaration.
I'd pick something a little more meaningful than just a, though, although that's not your problem.
a=2;
etc., will fix that reference issue. Maybe use
function cost = fare(dist,age)
or somesuch similar variable???
Is the distance actually rounded in the fare calculation or is it for any fraction of a mile over the previous full mile? The latter would seem more typical if not going to use actual mileage to nearest tenth or whatever...
  2 件のコメント
Stephen23
Stephen23 2018 年 6 月 16 日
Sudarshan Agrawal's "Answer" moved here:
I m still receiving the same problem. But the code is working for fare(0.2,67) only. the distance is rounded off to the nearest integer for the calculation
dpb
dpb 2018 年 6 月 16 日
編集済み: dpb 2018 年 6 月 16 日
Yes, because when you used fare as a variable inside the function you aliased the function fare so it no longer is visible as a function; now it is a local variable.
As I said, "you can't do that!", Matlab is not Fortran.
But what are the expected answers for
fare(1.2,67)
fare(1.7,67)
??

サインインしてコメントする。


Sudarshan Agrawal
Sudarshan Agrawal 2018 年 6 月 17 日
編集済み: Walter Roberson 2018 年 6 月 17 日
function cost = fare(dist,age)
x=round(dist);
if x>=0 && x<=1
f=2;
if x>1 && x<=10
f=2+(0.25*(x-1));
if x>=11
f=4.25+(0.1*(x-10));
end
end
end
if age<=18 || age>=60
cost=0.8*f;
else
cost=f;
end
end
The new code I tried!! It is working for fare(1.2,67) but not for fare(1.7,67).
  4 件のコメント
Walter Roberson
Walter Roberson 2018 年 6 月 17 日
With the structure you had, the test x>1&&x<=10 could only be made if x>=0&x<=1 had succeeded. The scope of the successful test if the first if extends until the matching "end" or "else" or "elseif"
dpb
dpb 2018 年 6 月 17 日
And there'll still be a similar problem with the x>=11 portion

サインインしてコメントする。


Sarthak Shukrey
Sarthak Shukrey 2019 年 6 月 18 日
my question doesnt demand for any 'if or else' statements , what will be the solution then ? TIA

Nimra Mubeen
Nimra Mubeen 2019 年 9 月 5 日
function fare =taxi_fare(d,t)
fare=5+(ceil(d-1)*2)+(ceil(t)*.25)
  2 件のコメント
Gopichand Kunamaneni
Gopichand Kunamaneni 2019 年 9 月 25 日
i could not understand this function. explain me in detail if possible. many thanks in advance

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by