Solving set of differential equations

2 vues (au cours des 30 derniers jours)
Mimo T
Mimo T le 25 Mai 2022
Modifié(e) : Torsten le 28 Mai 2022
I made these equations on simulink and their outputs are correct, but making them on .m file is really mess for me
y,z,u are my dependent variables.
x is 0.007*sin(4*pi*t)
so i solve EQ5 then subs in 3 after that i solve 2&4 together and subs back into 1 to get f
but plotting f with time is really wrong... this is the code
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tspan = [0 2];
u0 = 0; v=1.5; eta=190; n=2 ; co_a=784; co_b=1803; ko=3610;
c1_a=14649; c1_b=34622; k1=840; xo=0.0248; alpha_a=1241; alpha_b=38430;
beta=2059020; gamma=136320; A=58;
%%%%%%% Eqn 2 & 4
yZero = [0,0]; % and an initial condition
[T,myY]=ode45(@Eqn2_4,tspan,yZero); % solve
[t,u] = ode45(@(t,u) -eta*(u-v), tspan, 0);
y=myY(:,1);
z=myY(:,2);
k1=840;
x= 0.007*sin(4*pi*t);
xdot= 0.007*4*pi*cos(4*pi*t);
y = interp1(T,y,t);
z = interp1(T,z,t);
c1=c1_a+c1_b.*u; %%eqn3
co=co_a+co_b.*u; %%eqn3
alpha=alpha_a+alpha_b.*u; %%eqn3
ydot=(1./(co+c1).*((alpha.*z)+(co.*xdot)+ko.*(x-y)));
fa=c1.*(ydot)+k1*(x-xo);
plot(t,fa)
%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
and the function Eqn2_4 is
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [ret]=Eqn2_4(t,y)
% set up ret in correct form (i.e. a column vector)
ret=zeros(2,1);
u0 = 0; tspan = [0 2];
v=1.5;
eta=190; n=2 ;
co_a=784; co_b=1803; ko=3610;
c1_a=14649; c1_b=34622; k1=840;
xo=0.0248; alpha_a=1241; alpha_b=38430;
beta=2059020; gamma=136320; A=58;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[t,u] = ode45(@(t,u) -eta*(u-v), tspan, u0);
alpha=alpha_a+alpha_b.*u; %%eqn3
c1=c1_a+c1_b.*u; %%eqn3
co=co_a+co_b.*u; %%eqn3
% extract current values of V and P
y1 = y(1);
z = y(2);
%%%%%%%%%%%%%%%%%%%%%%%%%%%
alpha=interp1(u,alpha,t);% Interpolate the data set
c1 = interp1(u,c1,t); % Interpolate the data set (u,c1) at time t
co = interp1(u,co,t); % Interpolate the data set (u,co) at time t
x= 0.007*sin(4*pi*t);
xdot= 0.007*4*pi*cos(4*pi*t);
% return updates
ret(1) = (1./(co(1)+c1(1)).*((alpha(1).*z)+(co(1).*xdot(1))+ko.*(x(1)-y1)));
ret(2) = (-gamma.*abs(xdot(1)-ret(1)).*abs(z))-(beta.*(xdot(1)-ret(1)).*pow2(abs(z)))+50*(xdot(1)-ret(1));
end
  2 commentaires
Torsten
Torsten le 25 Mai 2022
Solve (2),(4) and (5) together in one call to ode15i.
Mimo T
Mimo T le 26 Mai 2022
Thanks i am trying it

Connectez-vous pour commenter.

Réponse acceptée

William Rose
William Rose le 25 Mai 2022
@Torsten is exactly right, and @Torsten is incredibly good at quickly seeing the key thing to do.
In case it is not obvious, you must re-write your equations using two vectors, let's call them y and yp (for y prime)
,
where y and yp on the left are the new y and yp, which you will pass to ode15i, and the y,z,u on the right are the old names for the variables you want to solve for.
Rewrite equations 2, 4, 5, using the new y and yp. As you do this, replace x with 0.007*sin(4*pi*t) and replace xdot with 0.028*pi*cos(4*pi*t), and replace α with , and replace with , and replace with , and write equations 2, 4, 5 so that you have zero on one side and everything else on the other side. Then equations 2, 4, 5 become
(You can finish equation 4.)
Now the equations are in the form that you need in order to write the function you will pass to ode15i:
function res = mydiffeq(t,y,yp)
%MYDIFFEQ returns residual of my implicit differential equations
eta=190; n=2; k0=3610;
c0a=784; c0b=1803;
c1a=14649; c1b=34622;
alphaa=1241; alphab=38430;
beta=2059020; gamma=136320;
res=[yp(1)-((alphaa+alphab*y(3))*y(2)+(c0a+c0b*y(3))*0.028*pi*cos(4*pi*t)+k0*(0.007*sin(4*pi*t)-y(1)))/(c0a+c0b*y(3)+c1a+c1b*y(3));...
yp(2)+gamma*abs(0.028*pi*cos(4*pi*t)-yp(2))*abs(y(2))^(n-1)*y(2)+beta*(...);...
yp(3)+eta*(y(3)-nu));
end
Read the help for ode15i, especially the examples for the Weissinger problem and the Robertson problem. THey will show you how to use ode15i.
Check all my equations and code above for possible errors. Try it. Good luck.
  9 commentaires
William Rose
William Rose le 27 Mai 2022
You are corrct. My mistake. I would compute numerically:
%Next: Compute a first order estimate of the derivative of y(1,:)
ydot=diff(y(:,1))./diff(t);
%Next: compute f_a
f_a=(c1a+c1b*y(2:end,3)).*ydot+k1*(0.007*sin(4*pi*t(2:end))-x0);
I use y(2:end,3) and t(2:end) because diff(y(:,1)) and diff(t) returns N-1 elements, if y and t have N elements. Therefore ydot will have N-1 elements. Try it.
Torsten
Torsten le 27 Mai 2022
Modifié(e) : Torsten le 28 Mai 2022

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by