Error in Matlab ode45
显示 更早的评论
function dxdt = amanda1(t,x)
dxdt = zeros(2,1);
dxdt(1) = (1/40)*(3.01 - x(1)) - 10000 * exp(-4000/x(2) )* x(1)
dxdt(2) = (1/40)*(257 - x(2)) - (-50/0.99) * (10000 * exp(-4000/x(2)) * x(1));
function [T, X] = amanda2(t,x)
tspan = [0:50:300];
initial = [0.5 250];
[T, X] = ode45(@amanda1,tspan,initial);
fig(1), plot(T,X(:,1), 'o'), title('[CO2] against time'), ylabel('[CO2]'), xlabel('[Time]')
hold on
fig(2), plot(t, x(:,2), '*'), title('T against time'), ylabel('[T]'), xlabel('[Time]')
Uberzyme_System amanda2
i'm trying to plot co2 against time and t versus time using ODE45 differential equations, however this error message kept showing up.
Error in CW1Q3 (line 3) dxdt(1) = (1/40)*(3.01 - x(1)) - (10000 * exp(-4000/x(2)) * x(1))
>> CW1Q3 Not enough input arguments.
Error in CW1Q3 (line 3) dxdt(1) = (1/40)*(3.01-x(1))-10000 * exp(-4000/x(2)) * x(1)
回答(1 个)
Star Strider
2018-1-5
I suspect the errors in your code were obvious to Torsten.
The problem is your ‘amanda2’ function. Functions have their own workspaces, and (except in some specific instances), do not share them with other functions, or with your MATLAB workspace. So the information in ‘amanda2’ is not available to ode45.
With these small changes, your code works for me:
function dxdt = amanda1(t,x)
dxdt = zeros(2,1);
dxdt(1) = (1/40)*(3.01 - x(1)) - 10000 * exp(-4000/x(2) )* x(1);
dxdt(2) = (1/40)*(257 - x(2)) - (-50/0.99) * (10000 * exp(-4000/x(2)) * x(1));
end
tspan = [0:50:300];
initial = [0.5 250];
[T, X] = ode45(@amanda1,tspan,initial);
figure(1), plot(T,X(:,1), 'o'), title('[CO2] against time'), ylabel('[CO2]'), xlabel('[Time]')
figure(2), plot(T, X(:,2), '*'), title('T against time'), ylabel('[T]'), xlabel('[Time]')
Note that if you have all your code in a function (not in a script file), you can run it as I posted it. If you are running it in a script file (rather than in a function file), you have to save ‘amanda1’ to a file named ‘amanda1.m’ on your MATLAB user path. Your code will then work.
类别
在 帮助中心 和 File Exchange 中查找有关 App Building 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!