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 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