The following error occurred converting from sym to double: Unable to convert expression into double array.
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Hi!
I am testing a function in MATLAB: ode45.
I created some functions in a class to run this testing, because I am development a software in App Designer and I need to realize this test in this organization.
Well, when I run this code, an error appears about converting from sym to double in my parameter P. I put the command double() or vpa(), but the error always apperars.
Can you help me, please? Thanks!
Next, the code:
classdef Teste < handle
methods (Static)
function parameters = eq()
syms t
P = t - exp(t);
K1 = 2;
K3 = 3;
parameters = [P, K1, K3];
end
function dydt = eq1(y)
parameters = Teste.eq();
P = parameters(1);
K1 = parameters(2);
K3 = parameters(3);
dydt = zeros(2,1);
dydt(1) = y(2);
dydt(2) = P - K1 * y(1) - K3*(y(1)^3);
end
function graph
tspan = [0 5];
y0 = [0 0.02];
[t,y] = ode45(@(t,y) Teste.eq1(y), tspan, y0);
plot(t,y(:,1),'-o',t,y(:,2),'-.')
end
end
end
0 comentarios
Respuestas (1)
Walter Roberson
el 5 de En. de 2020
zeroes creates a numeric array. Your parameters are constructed using symbolic t so the second assignment is trying to assign a symbolic expression with unresolved variable into a numeric slot in the array.
2 comentarios
Walter Roberson
el 5 de En. de 2020
[t,y] = ode45(@(t,y) Teste.eq1(y), tspan, y0);
Make that
[t,y] = ode45(Teste.eq1, tspan, y0);
And make eq1 return a function handle that takes two parameters. It can construct the function handle by using matlabFunction with the 'vars' parameter.
Ver también
Categorías
Más información sobre Symbolic Math Toolbox en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!