Matlab code error

Asked

Viewed 119 times

0

Can someone please give me a hand? I’m trying to make a program on Octave and it’s giving me the following mistake:

max recursion Depth exceeded

error Caled from

Size at line 3 column 3

spending at line 2 column 5

First I implemented a function used to determine the expenditure of a vehicle, with a consumption c (in liters/100km) when travelling a given distance, dab (given in km), between two cities with altitudes ha and Hb (measured in meters).

function gc = gasto(ha,hb,dab,c)
  if dab == 0
    gc = 0;

  else
    gc = (c/100) * dab * (1+ 100*(hb-ha)/(1000*dab))^2;

  end;
end

Then I implemented a function, expended, to transform a distance matrix, indicated by the Graph parameter, into an expense matrix, C, using the worn function, applicable to non-zero distances.

function G = gastos(Grafo,Altitudes,consumo)
  n = size(Grafo,1);
  G = zeros(n,n);

  for i = 1 : n
    for j = 1 : n

     if Grafo(i,j) == 0;
       G(i,j) = Inf;

     else
       G(i,j) = gasto(Altitudes(i),Altitudes(j),Grafo(i,j),consumo);

     end;
  end;
end

i used Graph = [0.102,89,0,136; 102,0,157,172,39; 89,157,0,58,0; 0,172,58,0,95; 136,39,0,95,0] Altitudes = [100, 160, 85, 90, 130] consumption = 6.5 and when calling the function expended(Graph, Altitudes, consumption) gave me this error , but I did not notice why? inserir a descrição da imagem aqui inserir a descrição da imagem aqui

someone can tell me why this mistake is coming up?

1 answer

0

This seems to me an error in the function input size.

What is the size of the variable Grafo?

If the variable Grafo is empty, it has zero dimension. If you ask for the number of variables in dimension 1 it will have problems.

max recursion Depth

is a system variable which may be being called, if the Grafo is empty, in this case dimension 1 is larger than the dimension limit. But this is just an idea.

Unfortunately the function manual size does not give more details.

Running with the values added to your question, I got

G=[       Inf    7.4329    5.5916       Inf    9.2343
    5.8729       Inf    9.2533   10.2885    2.1600
    5.9816   11.2033       Inf    3.8353       Inf
       Inf   12.1085    3.7053       Inf    6.7059
    8.4543    2.9400       Inf    5.6659       Inf]

Which is what I expected (I didn’t check the figures).

  • Okay, I’ve done it.

  • @aluno20000, I can’t reproduce your error! And I still think you have data entry problems. Vc can reproduce the error with this or another [mcve]?

  • I didn’t understand the question. I already put the code I used on top, what else am I supposed to do? You used Octave?

  • @aluno20000, yes, I used Octave. Can you reproduce this error with another example? See [mcve] for an idea. Example, if you don’t do the functions and write the whole code at once (simple as it’s just copies and paste) you have the error?

  • If delete: Function gc = spent(ha,Hb,dab,c) and Function G = spent(Graph,Altitudes,consumption) I get the error: invalid use of script c: spent users. m in index Expression, which does not surprise me because there are no functions in the code... I had never seen this error before

  • @aluno20000, I gave an option to have a [mcve], but you may have other ideas! : D Debugging is an important and difficult part of programming. New code will be welcome. Also, see that I recommended writing the whole code again, without the use of functions. Only deleting function statements should, a priori, give problems yes.

  • Thanks for the help, but then why did you recommend deleting : Function gc = spent(ha,Hb,dab,c) and Function G = spent(Graph,Altitudes,consumption), if you knew the code would not work?

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.