How can I change multiple variable name within a loop, while assigning those variables as matrix values?

2 visualizaciones (últimos 30 días)
I have a n x 3 matrix called dataMat. I also have n amount of processes that this code will eventually be able to handle. Each process has a n value (n), pressure (p) and volume (v). I want to be able to calculate the work done using 1 of two formulas-formula chosen based on if n=1 or not. My code is below, I don't know how to change the variable names while pulling data from the matrix at the same time.
n=size(dataMat,1);
for i=1:n
Cyc(i)n=dataMat(n,1);
Cyc(i)p=dataMat(n,2);
Cyc(i)v= dataMat(2,3);
end
formulas: if n=1 W=(P1)(V1)ln(V2/V1)
if not W=(P2*V2-P1*V1)/1-n1
  3 comentarios
Krish Desai
Krish Desai el 5 de Sept. de 2016
Your edits to #1 are correct. #2 is correct as is, with (1-n1) as the denominator.

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 4 de Sept. de 2016

Más respuestas (1)

John BG
John BG el 5 de Sept. de 2016
the initial extraction does not need a for loop:
Cyc_n=dataMat(:,1);
Cyc_p=dataMat(:,2);
Cyc_v=dataMat(:,3);
Since it looks like the syntax of the formulas needs improving, I guess your are trying to differentiate,
W=zeros(1,length(Cyc_v))
for k=2:1:length(Cyc_n)
W(k-1)=Cyc_p(k-1)*Cyc_v(k-1)*log(Cyc_v(k)/Cyc_v(k-1))
end
or
for k=2:1:length(Cyc_n)
W(k-1)=Cyc_p(k)*Cyc_v(k)-Cyc_p(k-1)*Cyc_v(k-1)/(Cyc_n(k)-Cyc_n(k));
end
Krish, if you find this answer useful would you please be so kind to mark my answer as Accepted Answer?
To any other reader, please if you find this answer of any help solving your question,
please click on the thumbs-up vote link,
thanks in advance
John BG

Categorías

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

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by