How to run an ODE Function with a variable outside of the Differential Equation
2 次查看(过去 30 天)
显示 更早的评论
Hi,
I am trying to solve and model an ODE function. The working code is provided and works exactly as I need, but now I want to vary the I term. I want to vary I so that it changes accordingly to the x. So for each x value to be a different I that is used. Something like the second code I provided, but it does not work.
The working code is provided:
function plotGammaComparison()
clc
clear all %#ok<CLALL>
figure
hold on;
R0 = .3;
L = 2*pi*R0;
Q = 0.01;
I = 0.0006;
x = 0:0.001:L;
gammas = [100, 10, .1, .01, .001];
for i = 1:length(gammas)
gamma = gammas(i);
fun = @(y, s) [gamma*Q/(I*s(1)) + 1; sin(s(1)); cos(s(1))];
tspan = x;
Y0 = [pi/2 0 0];
[x, y] = ode45(fun, tspan, Y0);
X = y(:, 2);
Y = y(:, 3);
plot(X, Y, 'DisplayName', sprintf('\\gamma = %.4f', gamma));
end
xlabel(' ');
ylabel(' ');
legend('Location', 'Best');
hold off;
end
The attempted code:
clc
clear all %#ok<CLALL>
figure
hold on;
R0 = .3;
L = 2*pi*R0;
Q = 0.01;
x = 0:0.001:L;
I = -4e8 * x + 4e10;
gammas = [100, 10, .1, .01, .001];
for i = 1:length(gammas)
gamma = gammas(i);
fun = @(y, s) [gamma*Q/(I*s(1)) + 1; sin(s(1)); cos(s(1))];
tspan = x;
Y0 = [pi/2 0 0];
[x, y] = ode45(fun, tspan, Y0);
X = y(:, 2);
Y = y(:, 3);
plot(X, Y, 'DisplayName', sprintf('\\gamma = %.4f', gamma));
end
xlabel(' ');
ylabel(' ');
legend('Location', 'Best');
hold off;
0 个评论
采纳的回答
Sam Chak
2024-2-28
@Adam, Do you expect the results as shown?
figure
hold on;
R0 = .3;
L = 2*pi*R0;
Q = 0.01;
x = 0:0.001:L;
% I = - 4e8*x + 4e10;
gammas = [100, 10, .1, .01, .001];
for i = 1:length(gammas)
gamma = gammas(i);
fun = @(y, s) [gamma*Q/((- 4e8*y + 4e10)*s(1)) + 1; % I = - 4e8*x + 4e10;
sin(s(1));
cos(s(1))];
tspan = x; % if x equals to y...
Y0 = [pi/2 0 0];
[x, y] = ode45(fun, tspan, Y0); % Confusing! Are x = y and y = s?
X = y(:, 2);
Y = y(:, 3);
plot(X, Y, 'DisplayName', sprintf('\\gamma = %.4f', gamma));
end
xlabel(' ');
ylabel(' ');
legend('Location', 'Best');
hold off;
5 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Particle & Nuclear Physics 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!