0
$\begingroup$

I have code generated by AI Aria in Opera browser

The code gives wrong answer only for: Depreciation factor=1?

assetValue=10000; salvageValue=2000; assetLife=5; depreciationFactor=1; decliningBalanceDepreciation[assetValue_, salvageValue_, assetLife_, depreciationFactor_] := Module[{bookValue = assetValue, depTable = {}}, AppendTo[ depTable, {"Year", "Beginning Book Value", "Depreciation Percent", "Depreciation Amount", "Accumulated Depreciation", "Ending Book Value"}]; Do[depreciation = Min[bookValue - salvageValue, depreciationFactor*bookValue/assetLife]; accumulatedDepreciation = If[i == 1, depreciation, Last[depTable][[5]] + depreciation]; endingBookValue = Max[salvageValue, bookValue - depreciation]; AppendTo[ depTable, {i, bookValue, (depreciation/bookValue)*100, depreciation, accumulatedDepreciation, endingBookValue}]; bookValue = endingBookValue;, {i, 1, assetLife}]; depTable] Grid[decliningBalanceDepreciation[assetValue, salvageValue, assetLife, depreciationFactor], Frame -> All] //N 

See below:

enter image description here

I use this Calculator to compute values.

Where is the mistake in the code?

Thanks.

$\endgroup$
9
  • $\begingroup$ Changing the depreciation to depreciation = If[i == assetLife, bookValue - salvageValue, Min[bookValue - salvageValue, depreciationFactor*bookValue/assetLife]] gives the expected result for the last record in the table {{4096., 51.1719, 2096., 8000., 2000.}} $\endgroup$ Commented Jan 14, 2024 at 21:43
  • $\begingroup$ What was your question to AI Aria so that it generated this code? $\endgroup$ Commented Jan 14, 2024 at 21:58
  • $\begingroup$ @azerbajdzan "Generate code For Depreciation with Declining Balance method ? With Asset cost=10000, Salvage Value=2000,Depreciation years=5 ,Depreciation factor=2, in Wolfram language ?" $\endgroup$ Commented Jan 14, 2024 at 22:01
  • $\begingroup$ @Mariusz Iwaniuk: So it looks that AI then found that link of the calculator and tried to translate JavaScript from that link into Wolfram language? Or you gave it also that link? $\endgroup$ Commented Jan 14, 2024 at 22:07
  • 1
    $\begingroup$ @Mariusz Iwaniuk: This calculator calculatorsoup.com/calculators/financial/… for example calculates the same Depreciation Amount of 819.20 for 5th year as in original code. So which calculator is correct now? $\endgroup$ Commented Jan 14, 2024 at 22:20

1 Answer 1

1
$\begingroup$

Here is my attempt at this. The function outputs a Dataset, which can of course be changed into lists of values as needed. I did not create the redundant entries in the data. For each year there is a year number, the beginning book value, the depreciation for that year (which will be subtracted from the book value for next years book value), and the accumulated depreciation as of the end of the year.

decliningBalanceDepreciation[assetValue_, salvageValue_, assetLife_Integer, depreciationFactor_] := Module[{depreciationRate, bv, n, dep}, depreciationRate = depreciationFactor/ assetLife; (* initialize depTable with year 1 *) depTable = {<| "year" -> 1, "beginningBookValue" -> assetValue, "depreciation" -> assetValue depreciationRate, "accumulatedDepreciation" -> assetValue depreciationRate |>}; (* year counter *) n = 1; (* loop through years *) Until[n > assetLife - 1, n++; (* new book value *) bv = depTable[[n - 1, "beginningBookValue"]] - depTable[[n - 1, "depreciation"]]; (* tentative depreciation *) dep = bv depreciationRate; (* next book value cannot drop below salvage, reduce depreciation if needed *) If[ (dep + depTable[[n - 1, "accumulatedDepreciation"]]) > ( assetValue - salvageValue), dep = - depTable[[n - 1, "accumulatedDepreciation"]] + assetValue - salvageValue ]; (* calculation done for year, add to depTable *) AppendTo[ depTable, <| "year" -> n, "beginningBookValue" -> bv, "depreciation" -> dep, "accumulatedDepreciation" -> depTable[[n - 1, "accumulatedDepreciation"]] + dep |> ]; ]; (* return depTable as Dataset *) Dataset@ depTable ] decliningBalanceDepreciation[3000, 500, 5, 2] 

enter image description here

$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.