Cost of Gas

Estimate the total cost of gas over the life of a vehicle.
Updated 6 Aug 2012

View License

My Dear Old Dad has a 1980 Ford F100 pickup truck that recently ticked past the quarter million mile mark. When I think of that truck I think of Saturday trips to the hardware store, I think of learning to drive, and I think of absolutely lousy gas mileage. So with the recent passing of the quarter million milestone I got to wondering, "How much money has Dad spent filling up this old rust bucket?"

The Matlab function here looks up gas price data and the consumer price index from the United States Bureau of Labor and Statistics website and estimates the present value of the total amount spent on gas over the life of the vehicle. "Estimates" is a key word here--it's not exact. I made some simplifying assumptions and I claim no expertise in economics.

year_or_range = The year you bought your car OR the years you had it.
miles_driven = The number of miles you've driven your car.
mpg = Average gas mileage of your car (miles per gallon).

An estimated present value (adjusted for inflation) of the sum of your gas receipts over the life of your vehicle. The function also creates a figure in which the price of gasoline and the present value of the cumulative sum spent on gas are plotted.

EXAMPLE 1, Dad's truck:

ans =

Question answered. My Father has spent about $50k filling up that Ford.

What about a car you sold a few years ago? Enter the dates you acquired and sold the car as an array. Consider a vehicle purchased in 2000, driven 25,000 miles, and sold in 2007.

EXAMPLE 2, Formerly-owned vehicle:
costofgas([2000 2007],25000,17)

ans =

Note: Scraping the BLS ftp site takes some time. Give the script about 60 seconds to run.

Cite As

Chad Greene (2024). Cost of Gas (, MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2011a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Find more on Oil, Gas & Petrochemical in Help Center and MATLAB Answers

Inspired: Inflation Adjust

Community Treasure Hunt

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

Start Hunting!
Version Published Release Notes

This version now allows for a range of dates during which a vehicle was owned.