How to run an ODE Function with a variable outside of the Differential Equation

3 visualizzazioni (ultimi 30 giorni)
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;

Risposta accettata

Sam Chak
Sam Chak il 28 Feb 2024
@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 Commenti
Adam
Adam il 28 Feb 2024
So yes, x=y and y=s in that case. Apologies on my end.
And yes that is exactly the trend of the I as I expect. That should be entered correctly. But i do see the issue lays with the I magnitude now and overshadows the other variables. Your edits were absolutely perfect and I understand that I just needed to integrate the I function into the ODE.
thank you so much for the help!
Sam Chak
Sam Chak il 28 Feb 2024
Good to hear that, @Adam. Try figuring out the reasonable range of I to obtain the desired results. If you find the solution helpful, please consider clicking 'Accept' ✔ on the answer and voting 👍 for it.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Particle & Nuclear Physics in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by