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
0 件のコメント
回答 (1 件)
  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 件のコメント
  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 Exchange で Symbolic Math Toolbox についてさらに検索
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

