Using Ode45 and Ode15 for second order

1 vue (au cours des 30 derniers jours)
Jessica Hamilton
Jessica Hamilton le 17 Juin 2016
I'm completely new to matlab and need to solve a second order differential equation with different values and plot it all on the same graph. See the attached image for the specific function and values. Below is what I have done so far and I am able to produce a graph for the first value of b, but I would like to set up a for loop so that it runs all the values and plots it in one go.
function [] = Matlab3()
tspan=[0 100]; % set time interval
y0=[0,3];% set initial conditions (y,y_dot)
[t,y] = ode45(@vdp1,tspan,y0); % call ode45
plot(t,y(:,1))
function dydt = vdp1(t,y) % define rhs function
b = [.1 1 10 100 1000]; % set constants
for i=1:5
y1=y(1); % allocate y1
y2=y(2); % allocate y2
dydt = [y2; (b(i)*(1-y1^2)*y2)-y1]; % set x_dot vector = [A]
end
end
end
  1 commentaire
Steven Lord
Steven Lord le 17 Juin 2016
This looks an awful lot like a homework assignment. In general Answers posters require people posting homework assignments, or questions that sound like homework assignments, to show what they've tried and ask a SPECIFIC question related to where they're having trouble.

Connectez-vous pour commenter.

Réponses (1)

Steven Lord
Steven Lord le 17 Juin 2016
You're close, but you have your for loop in the wrong place. You don't want to iterate inside your ODE function, you want to call ode45 once per value of your parameter b. So your loop should be inside Matlab3 but not inside vdp1. [The fact that you've written vdp1 as a nested function helps you; you don't need to pass the variable you want to "share" between Matlab3 and vdp1 into vdp1 as an input argument.]
I have one other hint for the next problem I anticipate you encountering: you may find the hold function useful.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by