The following error occurred converting from sym to double: Unable to convert expression into double array.

2 ビュー (過去 30 日間)
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

回答 (1 件)

Walter Roberson
Walter Roberson 2020 年 1 月 5 日
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 件のコメント
Ana Reis
Ana Reis 2020 年 1 月 5 日
Yeah, I understand. But, how can I resolve this? Because I put a syms array and the function ode45 not receive a symbolic parameter.
Walter Roberson
Walter Roberson 2020 年 1 月 5 日
[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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSymbolic Math Toolbox についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by