(ODE45) Unable to perform assignment because the left and right sides have a different number of elements
1 回表示 (過去 30 日間)
古いコメントを表示
Hi guys. I have a problem about publishing the ODE45 function and plot.
%% Solving the system of non-linear differential equations
function dx = HW4_matlab(t,x)
dx = zeros(2,1);
dx(1) = exp(-x(1))-x(2)*t;
dx(2) = x(1)*t+cos(x(1)+x(2));
[t,x] = ode45(@HW4_matlab,[1,5],[10,2]);
plot(t,x(:,1),'-b',t,x(:,2),'--r')
When I am going to publish this code, an error shows up:
Unable to perform assignment because the left and right sides have a different number of elements.
Error in HW4_matlab (line 4)
dx(1) = exp(-x(1)) - x(2)*t;
How could I fix it?
0 件のコメント
回答 (2 件)
Star Strider
2019 年 10 月 1 日
The code you posted runs for me with out error in R2019b.
The one change I made is to add an end statement to the function:
%% Solving the system of non-linear differential equations
function dx = HW4_matlab(t,x)
dx = zeros(2,1);
dx(1) = exp(-x(1))-x(2)*t;
dx(2) = x(1)*t+cos(x(1)+x(2));
end
[t,x] = ode45(@HW4_matlab,[1,5],[10,2]);
plot(t,x(:,1),'-b',t,x(:,2),'--r')
3 件のコメント
Star Strider
2019 年 10 月 1 日
Without more information, I have no idea what the problem could be that you are experiencing. As I mentioned, with that one change, your code runs for me withoout error. (I am running it inside a simple function that I use for some Answers problems.)
James Tursa
2019 年 10 月 1 日
編集済み: James Tursa
2019 年 10 月 1 日
It might be simpler to have separate files for this.
Put this code (and only this code) in a file called HW4_matlab.m
%% Solving the system of non-linear differential equations
function dx = HW4_matlab(t,x)
dx = zeros(2,1);
dx(1) = exp(-x(1))-x(2)*t;
dx(2) = x(1)*t+cos(x(1)+x(2));
end
And put this code in a separate file for calling that function
[t,x] = ode45(@HW4_matlab,[1,5],[10,2]);
plot(t,x(:,1),'-b',t,x(:,2),'--r')
参考
カテゴリ
Help Center および File Exchange で Ordinary Differential Equations についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!