How do I solve an ode and plot a graph of two terms within the ode

1 次查看(过去 30 天)
I have to plot gamma dot vs tau
where gamma dot can be defined in the range logspace(-3,3,100)
my attempt at this is as follows:
clc, clearvars, close all
eta0 = 8080;
lambda = 1.109;
gamma = logspace(-3, 3, 100);
f = @(t,tau)(-(tau+eta0*gamma)/lambda);
[t, tau] = ode45(@f,[0.001 1000], 0);
loglog(gammaint,tau,'.')
and ive recieved the error messages as follows:
Undefined function 'f' for input arguments of type 'double'.
Error in odearguments (line 92)
f0 = ode(t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 104)
odearguments(odeIsFuncHandle,odeTreatAsMFile, solver_name, ode,
tspan, y0, options, varargin);
Error in maxwell5 (line 9)
[t, tau] = ode45(@f,[0.001 1000], 0);
What can i do to fix this,
Thanks

采纳的回答

Steven Lord
Steven Lord 2023-11-16
eta0 = 8080;
lambda = 1.109;
gamma = logspace(-3, 3, 100);
f = @(t,tau)(-(tau+eta0*gamma)/lambda);
f is already a function handle. So there's no need to specify @f on the next line. Just pass f into ode45. But if you do that you run into a different problem:
[t, tau] = ode45(f,[0.001 1000], 0);
Error using odearguments
@(T,TAU)(-(TAU+ETA0*GAMMA)/LAMBDA) must return a column vector.

Error in ode45 (line 104)
odearguments(odeIsFuncHandle,odeTreatAsMFile, solver_name, ode, tspan, y0, options, varargin);
loglog(gammaint,tau,'.')
MATLAB calls the ODE function that you pass into ode45 with a vector of values for the second input. If you're trying to solve this for each value of gamma in turn, use a for loop over the elements of gamma. Define f using each element from gamma in turn then call ode45 using that function.
for k = 1:numel(gamma)
f = @(t,tau)(-(tau+eta0*gamma(k))/lambda);
[t, tau] = ode45(f, [0.001, 1000], 0);
% Do something with the t and tau created using this element of gamma
end
If instead you want gamma to be treated as a function of time whose value you only know at specific times, you're going to have to interpolate the value of gamma for the value of t with which ode45 calls the function. See the "ODE with Time-Dependent Terms" example on the ode45 documentation page for an example of this technique.
Finally, I don't know if it's just that you didn't show the definition of the variable or made a typo in the line, but your call to loglog throws an error because gammaint isn't defined.
  10 个评论
Namit Dayal
Namit Dayal 2023-11-23
I want to treat gamma(dot) as a vector of constants and solve for tau for all values of gamma(dot) separately
Torsten
Torsten 2023-11-23
That's simple because an analytical solution for tau exists in which you only need to insert the values for the parameters you want to use:

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Numerical Integration and Differential Equations 的更多信息

标签

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by