Make a loop with a function to vary one value

1 回表示 (過去 30 日間)
Jared Dube
Jared Dube 2022 年 11 月 26 日
回答済み: Torsten 2022 年 11 月 26 日
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 Center および File ExchangeProgramming についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by