필터 지우기
필터 지우기

Calculating the maintenance costs over (x) amount of years

조회 수: 3 (최근 30일)
Matthew Abarca
Matthew Abarca 2014년 11월 22일
답변: Matthew Abarca 2014년 11월 23일
Hey forum This is my first time using Mat-lab and the forums. I have been having trouble with both the conceptual aspect, Mat-lab syntax and the debugging the final solution the problem.
The question is asked as follows
A machine requires $2,000 in maintenance every 2 years, $3,500 maintenance every 5 years, and a major overhaul costing $11,300 every 7 years. Use a for-loop to determine the accumulated maintenance costs of the machine over the next x years. Assume that you are starting at Year 0 (i.e. first expense occurs at Year 2). Ex. accumYears(5) = 7500
Here is my attempt at a solution function
function sum = accumYears(x)
for i = 1:1:x
[q, r] = quorem(i,sym(2));
if r == 0
a(i) = 2000;
end
[q, r] = quorem(i,sym(5));
if r == 0
a(i) = a(i) + 5000;
end
[q, r] = quorem(i,sym(7));
if r == 0
a(i) = a(i) + 11300;
end
end
sum = sum(a);
end
I receive the error Undefined function or variable "sum".
Error in faccumYears (line 16) sum = sum(a);

답변 (2개)

MA
MA 2014년 11월 22일
sum is a built-in matlab function, you should name your function or matric something else like cost
cost = accumYears(x)
cost = sum(a)

Matthew Abarca
Matthew Abarca 2014년 11월 23일
I've tried the suggested changes and part of the test suite returns fail. On Matlab the code works until the next if statement nested in the for loop. I've recently learned about the mod function but would still like to better understand the quorem function. Why does the second input have to be sym(n) instead of just integer n. This is the error I receive when running the code:
Attempted to access a(5); index out of bounds because numel(a)=4.
Error in accumYears (line 9) a(i) = a(i) + 5000;
As a side note, the sum as output was placed there by default for the course and it seems to be during a week where the profs were tired of students hacking the test suites so a new test suit was added to detect cheating.
code
function cost = accumYears(x)
for i = 1:x
[q, r] = quorem(i,sym(2));
if r == 0
a(i) = 2000;
end
[q, r] = quorem(i,sym(5));
if r == 0
a(i) = a(i) + 5000;
end
[q, r] = quorem(i,sym(7));
if r == 0
a(i) = a(i) + 11300;
end
end
cost = sum(a);
end

카테고리

Help CenterFile Exchange에서 Data Import and Analysis에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by