Conversion to double from inline is not possible.
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
I have been trying to run this with my code, so I could get the true values and compare the error between the two methods such as
[s,w,truevals]=abm2(inline('(y/t)-(y/t)^2','t','y'),inline('t/(1+ln(t))','t'),1,2,1,0.1).
But I keep receiving an error like: "Conversion to double from inline is not possible."
[s,w,truevals] = abm2(f,g,a,b,y0,h)
%f=@(t,y)((y/t)-(y/t)^2);
%f1=@(s,w)((w/s)-(w/s)^2);
% h=0.1;
% a=1;
% b=2;
% y0=1;
n=(b-a)/h;
t(1,1)=a;
y(1,1)=y0;
w=zeros(n+1,1);
s=zeros(n+1,1);
s(1,1)=a;
w(1,1)=y0;
%RK4 method to get initial values
k1=h*f(t,y);
k2=h*f(t+(h/2),y+(k1/2));
k3=h*f(t+(h/2),y+(k2/2));
k4=h*f(t+h,y+k3);
y=y+(k1+(2*k2)+(2*k3)+k4)/6;
t=t+h;
w(2,1)=y;
s(2,1)=t;
truevals(1,1)=y0;
%Adams-Bashforth 2-Step
for i=2:n
y=y+(3/2)*h*f(s(i,1),w(i,1))-(h/2)*f(s(i-1,1),w(i-1,1));
truevals=g;
t=t+h;
w(i+1,1)=y;
s(i+1,1)=t;
end
0 comentarios
Respuestas (1)
Stephen23
el 12 de Feb. de 2017
Editada: Stephen23
el 12 de Feb. de 2017
Why use outdated and almost obsolete inline, which the MATLAB documentation specifically advises to avoid using?: "inline will be removed in a future release. Use Anonymous Functions instead."
Once you switch to much more reliable function handles then your code works fine:
>> [s,w,truevals]=abm2(@(t,y)(y/t)-(y/t)^2,@(t)t/(1+log(t)),1,2,1,0.1)
s =
1
1.1
1.2
1.3
1.4
1.5
1.6
1.7
1.8
1.9
2
w =
1
1.0043
1.0162
1.0317
1.0498
1.0697
1.091
1.1133
1.1364
1.16
1.184
truevals =
@(t)t/(1+log(t))
Note that in MATLAB the natural logarithm is written log, not ln, and that you never evaluate the function g but simply allocate it to an output, which is why it is returned as a function handle.
The file I used is attached here:
4 comentarios
Stephen23
el 12 de Feb. de 2017
Editada: Stephen23
el 12 de Feb. de 2017
@Chris Kwan: I ran your code. It works without error. This is where you need to learn how to debug code. Eventually all beginners have to learn how to debug their own code. Really, knowing how to debug code is part of learning how to write code.
Stop trying to do everything at once. Restart MATLAB, create a new working directory, do not put your old working directory on the MATLAB path. Use the file that is attached to my question. Run the code exactly as I did in my answer. It works, I just tried it again now.
"given true solution "g" to evaluate the values at every "t""
I have no idea what a "true solution" is. If you wish to evaluate the function g for each t value, then this is very easy because the function already returns a vector of t values (although the vector is confusingly named s), so you can simply do this:
>> [s,w]=abm2(@(t,y)(y/t)-(y/t)^2, [], 1,2,1,0.1);
>> truevalues = s./(1+log(s))
truevalues =
1
1.0043
1.015
1.0298
1.0475
1.0673
1.0884
1.1107
1.1337
1.1572
1.1812
Ver también
Categorías
Más información sobre Function Creation en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!