Make a loop with a function to vary one value

1 次查看(过去 30 天)
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

采纳的回答

Torsten
Torsten 2022-11-26
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by