I'm getting two answer from my function, I only want one. How do I fix this?
1 view (last 30 days)
Show older comments
function payment = fare(miles,age)
tot1 =0;
tot2=0;
tot3=0;
if miles <= 1
tot1 = 200;
payment = tot1;
elseif miles <=10
tot1= 200;
tot2 = (miles-1)*0.25*100
payment = tot1+tot2;
elseif miles>10
tot1 = 200
tot2 = (10-1)*0.25*100
tot3 = (miles-10)*0.10*100
payment = tot1+tot2+tot3;
end
payment = (tot1+tot2+tot3)/100;
if age <=18 || age>=65
payment = (tot1+tot2+tot3)*0.8/100;
else
payment=(tot1+tot2+tot3)/100;
end
end
This is the code that produces the two variables, while I only want one: payment.
How do I get it?
0 Comments
Answers (1)
Stephen23
on 26 Nov 2016
Edited: Stephen23
on 26 Nov 2016
Your code omly returns one variable, payment, but it will print several other variables to the command window because you did not put semi-colons at the end of the lines:
elseif miles <=10
tot1= 200;
tot2 = (miles-1)*0.25*100 % <- add semicolon
payment = tot1+tot2;
elseif miles>10
tot1 = 200 % <- add semicolon
tot2 = (10-1)*0.25*100 % <- add semicolon
tot3 = (miles-10)*0.10*100 % <- add semicolon
0 Comments
See Also
Categories
Find more on Quantum Mechanics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!