matlab - Numerical solution of coupled dif eq -
so have set of 3 differentialequations want solve. can seen in code. problem, want combine these codes can have loop respect r (as shown.
what have:
t2 = 1; [t,y] = ode45(@ball, [0 5*t2] ,[0 0 -10]); figure plot(t,y(:,1),'-r',t,y(:,2),'-g',t,y(:,3),'-b') legend('x(t)','y(t)','z(t)') xlabel('time (in units of t2)') title(['plot rt2 = ',num2str(r)]) where @ball
`function dr = ball(t,b) t2 = 1; t1 = t2/2; d = 0; r = 0.2; dr = zeros(3,1); dr(1) = (-1/t2)*b(1)-d*b(2); dr(2) = (-1/t2)*b(2) + d*b(1) + r*b(3); dr(3) = (-1/t1)*b(3) - r*b(2) ; end` what want single programme this, allow me include loop can vary r , create couple subplots. possible?
you can utilize anonymous function this.
change ball.m remove hard-coded r , replace input argument:
function dr = ball(t,b,r) t2 = 1; t1 = t2/2; d = 0; %// etc. and replace ode45 phone call this:
r=0.4; [t,y] = ode45(@(t,b) ball(t,b,r), [0 5*t2] ,[0 0 -10]); where @(t,b) ball(t,b,r) function inputs t , b calls ball.m value of r specified on previous line. can build for loop follows:
for r=0.2:.02:1 %// or whatever range want [t,y] = ode45(@(t,b) ball(t,b,r), [0 5*t2] ,[0 0 -10]); %// etc. end matlab differential-equations
No comments:
Post a Comment