I need help solving a equation with the Runge-Kutta-Fehlberg adaptive step size method using ode45.

3 次查看(过去 30 天)
I need help solving the last two problems. I attached my code for the first one and what I have of the second and third. In my command window, it is saying function_handle with my last two equations, and it will not run it to get the final answers. I need help fixing my code for the last two problems, and it must be solved using ode45.
% Problem 1
xspan = linspace(0.1,10);
for n = 1:10;
for x = 0.1:10;
roc = @(x,y) (-1^(n+1))./(x^n.*factorial(n));
[x, y] = ode45(roc, xspan, 0, odeopts);
end
end
fprintf('Problem 2 answer is %0.3f.\n', sum(y))
% Problem 2
xspan = linspace(0,4*pi);
for n = 0:4*pi;
roc = @(x, y) (cos(x/2)+sin(2*x)).^2
[x, y] = ode45(roc, xspan, 0, odeopts);
end
fprintf('Problem 3 answer is %0.3f.\n')
% Problem 3
xspan = linspace(0,pi);
for x = 0:pi;
roc = @(x,y) (cos(y)).^2 + (sin(x)).^2
[x, y] = ode45(roc, xspan, 0, odeopts);
end
fprintf('Problem 4 answer is %0.3f.\x')

回答(1 个)

James Tursa
James Tursa 2020-10-6
编辑:James Tursa 2020-10-6
For Problem 2 and Problem 3, you should not be looping over any variables. You simply have a derivative function that you pass to ode45 along with some other inputs. ode45 will do the looping over the variables internally. E.g.,
% Problem 2
xspan = linspace(0,4*pi);
roc = @(x, y) (cos(x/2)+sin(2*x)).^2;
[x, y] = ode45(roc, xspan, 0, odeopts);
and
% Problem 3
xspan = linspace(0,pi);
roc = @(x,y) (cos(y)).^2 + (sin(x)).^2;
[x, y] = ode45(roc, xspan, 0, odeopts);
You might want to save the results of Problems 2 and 3 under different variable names so they don't overwirte.
For Problem 1, the way I read the problem statement you actually have 10 different ode's to solve. That is, you have g1, g2, ..., g10 variables, each with their own ode involving a sum of terms. So you would need 10 different solutions. Can you clarify this?

类别

Help CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by