Make a loop with a function to vary one value

1 vue (au cours des 30 derniers jours)
Jared Dube
Jared Dube le 26 Nov 2022
Hi I'm currently attempting to make a loop that will make my function vary the eta value. I want it to plot all 5 variations on a single graph. The code can be found below. Any help would be very appreciated.
clearvars
close all
clc
tspan = [0 100];
y0 = 0;
opt = odeset('RelTol',1e-12,'AbsTol',1e-16,'MaxStep',0.001);
[t,y] = ode45(@myfunction,tspan,y0,opt);
plot(t,y); % trying to plot this for the function running a different value of eta each time
function dxdt = myfunction(tt,xx)
E1 = 1000;
E2 = 200;
eta = 10; %vary by 10, 50, 100, 500, 1000
F0 = 5e-10;
F = F0*sin(tt);
dfdt = F0*cos(tt);
dxdt = -E1*xx/(eta*(1+E1/E2))+(F+ eta/E2*dfdt)/(eta*(1+E1/E2));
end

Réponse acceptée

Torsten
Torsten le 26 Nov 2022
tspan = [0 :0.01:20];
y0 = 0;
opt = odeset('RelTol',1e-12,'AbsTol',1e-16,'MaxStep',0.001);
Eta = [10, 50, 100, 500, 1000];
for i = 1:numel(Eta)
eta = Eta(i);
[t,y] = ode45(@(t,x)myfunction(t,x,eta),tspan,y0,opt);
Y(:,i) = y;
end
plot(t,Y); % trying to plot this for the function running a different value of eta each time
function dxdt = myfunction(tt,xx,eta)
E1 = 1000;
E2 = 200;
F0 = 5e-10;
F = F0*sin(tt);
dfdt = F0*cos(tt);
dxdt = -E1*xx/(eta*(1+E1/E2))+(F+ eta/E2*dfdt)/(eta*(1+E1/E2));
end

Plus de réponses (0)

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by