Indexing Error Using 'dsolve'

13 ビュー (過去 30 日間)
Blake
Blake 2024 年 3 月 8 日
編集済み: Walter Roberson 2024 年 3 月 8 日
I'm trying to write a script to solve the differential equation and then display the solution with a few different initial conditions.
This is the code so far:
syms t k1 k2 M A0 A
% Differential equation
dA_dt = 1 * (20 - A) - 3 * A;
% Solving for A(t) with different initial conditions
A_sol_0 = dsolve(dA_dt, A(0) == 1);
A_sol_2 = dsolve(dA_dt, A(0) == 2);
A_sol_5 = dsolve(dA_dt, A(0) == 5);
A_sol_10 = dsolve(dA_dt, A(0) == 10);
% Displaying the solutions
disp('Solution for A0 = 0:');
disp(A_sol_0);
disp('Solution for A0 = 2:');
disp(A_sol_2);
disp('Solution for A0 = 5:');
disp(A_sol_5);
disp('Solution for A0 = 10:');
disp(A_sol_10);
And the error I keep getting is:
Array indices must be positive integers or
logical values.
Error in indexing (line 968)
R_tilde = builtin('subsref',L_tilde,Idx);
Error in ************ (line 7)
A_sol_0 = dsolve(dA_dt, A(0) == 1);
Help would be GREATLY appreciated.

回答 (1 件)

VBBV
VBBV 2024 年 3 月 8 日
編集済み: VBBV 2024 年 3 月 8 日
syms t k1 k2 M A0 A(t)
dA_dt = 1 * (20 - A) - 3 * diff(A,t) == 0;
% Solving for A(t) with different initial conditions
A_sol_0 = dsolve(dA_dt, A(0) == 0);
A_sol_2 = dsolve(dA_dt, A(0) == 2);
A_sol_5 = dsolve(dA_dt, A(0) == 5);
A_sol_10 = dsolve(dA_dt, A(0) == 10);
% Displaying the solutions
disp('Solution for A0 = 0:');
Solution for A0 = 0:
disp(A_sol_0);
disp('Solution for A0 = 2:');
Solution for A0 = 2:
disp(A_sol_2);
disp('Solution for A0 = 5:');
Solution for A0 = 5:
disp(A_sol_5);
disp('Solution for A0 = 10:');
Solution for A0 = 10:
disp(A_sol_10);
  1 件のコメント
VBBV
VBBV 2024 年 3 月 8 日
dA_dt = 1 * (20 - A) - 3 * A;% this is not a differential equation

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

カテゴリ

Help Center および File ExchangeMeasurements and Spatial Audio についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by