Syntax question - renaming a variable

1 visualización (últimos 30 días)
Lenilein
Lenilein el 28 de En. de 2019
Comentada: Lenilein el 28 de En. de 2019
Dear Community,
I have a very basic syntax question:
I defined up=y(1) because I wish that matlab evaluates the diff(up) for every k and the syntax diff(y(1)) doesn't seem to work.
However I realized that after solving that upALL=cat(1,upALL,up) and the first column of yALL (so yALL(1)) don't show the same values even though I expect them to be strictly same. Also I expect that yALL and upALL return the same number of values over the same interval LALL.
Any hint?
Thanks!!
Ld=2.8; Ld1=3.4; Lv=1.3; Lv1=1.5;
s=125; LPool1=zeros(s,1);
for k=1:numel(LPool1)
if rem(k,2)==1 && k<98
LPool1(k)= (Ld+Lv)*(k-1)/2;
elseif rem(k,2)==1 && k>97
LPool1(k)= (Ld+Lv)*(97-1)/2+(Ld1+Lv1)*(k-97)/2;
elseif k<98
LPool1(k)=(k/2)*Ld+(k/2-1)*Lv;
elseif k>97
LPool1(k)=(96/2)*Ld+(96/2)*Lv +((k-96)/2)*Ld1+((k-96)/2-1)*Lv1;
end
end
LPool=LPool1.';
y0=[1.1,40];
LALL=[];yALL=[];upALL=[];
for k=1:numel(LPool)-1
[L,y]=ode45(@(L,y) myODE(L,y),LPool(k:k+1),y0); y0=y(end,:);
up=y(1);
upALL=cat(1,upALL,up); LALL=cat(1,LALL,L); yALL=cat(1,yALL,y);
end
function dy = myODE(L,y)
global u; u=y(1);
global Tp; Tp=y(2);
dudL = myODE1(L,u,Tp);
dTpdL = myODE2(L,u,Tp);
dy = [dudL;dTpdL];
end
function dudL = myODE1(L,u,Tp)
dudL=5*u+3*Tp;
end
function dTpdL=myODE2(L,u,Tp)
dTpdL=12*u+25*Tp;
end

Respuesta aceptada

Guillaume
Guillaume el 28 de En. de 2019
"I defined up=y(1) because I wish that matlab evaluates the diff(up) for every k and the syntax diff(y(1)) doesn't seem to work"
There is absolutely no difference between:
diff(y(1))
and
up = y(1);
diff(up)
They will give the exact same result. In addition since in both case, you're only passing a scalar value you will always get [] as a result.
"[...] and the first column of yALL (so yALL(1))"
No. y(1) and yALL(1) is never going to be the first column. It is the first element only. To get the first column:
y(:, 1) %all the rows of the first column
yAll(:, 1)
This is probably the cause of all your problems.
Beside this, get rid of the two global statements in your code. They do absolutely nothing useful and your code would work exactly the same without it. In fact, your 2nd loop could actually be rewritten as:
ode = @(~, y) [5*y(1)+3*y(2); 12*y(1)+25*y(2)]; %replaces your three function myODE, myODE1, myODE2
LALL = []; yALL = [];
for k = 1:numel(LPool)-1
[L, y] = ode45(ode, LPool(k:k+1), y0);
LALL = [LALL; L]; %clearer than writing cat(1, LALL, L)
yALL = [yALL; y];
end
upALL = yALL(:, 1); %1st column of yALL.
  1 comentario
Lenilein
Lenilein el 28 de En. de 2019
Thanks Guillaume for your very clear explanations!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Programming en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by