-2

I wrote the code below and it worked for several weeks without any problem. Suddenely it stoped working with following error:

"warning: lsode: passing function body as a string is obsolete; please use anonymous functions
warning: called from
heun at line 23 column 2
error: 'f' undefined near line 1 column 42
error: lsode: evaluation of user-supplied function failed
error: called from
__lsode_fcn__C__ at line 1 column 40
heun at line 23 column 2"

The code is:

function yn=euler(t0,y0,tend,f,n)
t0=input('Gib hier t0 ein:');
y0=input('Gib hier y0 ein:');
tend=input('Bis zu welchem Wert von t möchtest du y annährn?');
n=input('Gib hier die Anzahl der Schritte ein(n):');
fstrich=input('Wie lautet die Differentialgleichung?', 's');
f=inline('fstrich');
dt=(tend-t0)/n;
t(1)=t0;
y(1)=y0;
for i=1:n
t(i+1)=t(i)+dt;
y(i+1)=y(i)+f(t(i),y(i))*dt;


rotdim([flip(t) ; flip(y)])
scatter(t,y,'*')
hold on
f=inline('fstrich');
t=t0:0.001:tend;
y=lsode('f',y0,t);
plot(t,y)

Does anyone see a mistake or something I can change?

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
Adn Zek
  • 1
  • 2

1 Answers1

1

Apparently you updated octave.

f=inline('fstrich'); % this is discouraged
y=lsode('f',y0,t); % and this is not valid anymore. 

Instead, make an anonymous function

f=str2func(fstrich);
y=lsode(f,y0,t);
Ander Biguri
  • 35,140
  • 11
  • 74
  • 120
  • i am getting this error: error: @y/(1+t^2): no function and no method found error: called from euler at line 7 column 2 >> – Adn Zek Sep 13 '20 at 17:26
  • @AdnZek right, because y/(1+t^2) is not valid indeed. Try `@(y,t)( y/(1+t^2) )` as input. – Ander Biguri Sep 13 '20 at 17:30
  • it works now but the plot of the solution is wrong. – Adn Zek Sep 13 '20 at 17:40
  • but this is not the solution of the dgl with the initial value (-3/1) – Adn Zek Sep 13 '20 at 17:40
  • Please stop spamming, I am not here for your service, I am helping you for free on my free time, be a bit mindful about that. In your code, you are trying to solve an equation for `y`. Why is then `y` part of the input you put in? would it not make sense that your input string has no dependency on `y`? I am not sure you udnerstand what you are trying to do, but if you do, can you try to explain it in the original post? – Ander Biguri Sep 13 '20 at 17:56
  • Sorry for that. I am not taking your help for granted! I tried it with another variable but it still doesnt work. Somehow the programm plots the wrong solution of the dgl. I think i understand what i am trying to do. i am solving a dgl with the euler method and then the exact solution with the lsode function. – Adn Zek Sep 13 '20 at 18:07
  • You have not answered the question that was relevant. Why is the output, `y`, part of the equation ? – Ander Biguri Sep 13 '20 at 18:10
  • I am not sure. I dont think it makes any difference. I changed it and it still didnt work. – Adn Zek Sep 13 '20 at 18:13
  • @AdnZek how could the equation you try to fit not make any difference? It should make *all* the difference. This is why I mean I don't think you know what you are doing – Ander Biguri Sep 13 '20 at 18:27
  • What would you recommend? – Adn Zek Sep 13 '20 at 18:31
  • You seem to not be understanding what I meant at all. It has nothing to do with what I do or dont recommend, its just what you are doing does not make sense, and its your mathematical problem to solve. I cant recommend what to solve, that makes no sense. – Ander Biguri Sep 13 '20 at 18:45
  • yes, i also think i dont unterstand what you mean. maybe because my english is not that good. but i have a master degree in math and would not say that i dont understand math. maybe if you give me a hint i would understand it better. but its okay, i dont want to waste youre time anymore. it seems like you know the solution but wont help me with it. – Adn Zek Sep 13 '20 at 18:57
  • @AdnZek what I am trying to say is that you have a 1 variable function to solve. a 1 variable function, is defined as `y=f(t)= something_that_does_math_on_t`. For example, `f=t^2`. so your function is `@(t)(t^2)`. Yet in the only example you provided (remember, a [mcve] is a requirement for debugging help), your function is `y/(t^2+1)`, i.e. `z=f(y,t)`. That is not a 1 variable function, thus you can not fit it to the 1 variable system you want to solve. I asked what is `y` 7 comments ago and you don't seem to be able to explain. Good luck – Ander Biguri Sep 13 '20 at 20:40