Collect values into an array

조회 수: 5 (최근 30일)
Phoebe
Phoebe 2014년 3월 28일
댓글: Phoebe 2014년 3월 28일
I have the following within my code :
while time<tfinal
for np = 1 : particleno
%Run Function to calculate guiding centre for diagnostic test
[a, b, gcx]=guidingcentrefun(x1(np), x2(np), x3(np), y1(np), y2(np), y3(np));
end
end
Where guidingcentrefun is as follows:
function [ a b gcx] = guidingcentrefun(x1, x2, x3, y1, y2, y3)
%Calculate guiding centre with simultaneous eqns
k1=(-2*x1)+(2*x2);
k2=(-2*y1)+(2*y2);
k3=(x2^2) -(x1^2) + (y2^2) - (y1^2);
k4=(-2*x1)+(2*x3);
k5=(-2*y1)+(2*y3);
k6=(x3^2)-(x1^2)+(y3^2)-(y1^2);
%coordinates of guiding centre
b=((k1*k6)-(k3*k4))/((k1*k5)-(k2*k4));
a=(k3-(k2*b))/k1;
gcx = [ a ];
end
I just want gcx to store ALL of the values of a in one array. I can run the code and let the values of gcx be outputted so I know it all works:
gcx =1.1139
gcx =1.1146
gcx =1.1153
gcx =1.1159
gcx =1.1165
gcx =1.1170
gcx =1.1175
I know this is probably quite simple but i cant get it right and it is pretty urgent so help would be so much appreciated!! please! Thanks in advance! Phoebe
  댓글 수: 3
Image Analyst
Image Analyst 2014년 3월 28일
Then why does np go from 1 to particleno? Also, there is no time compute in the loop. I'll try your code attached below but I need to go out for a few hours. Post back if you end up fixing it on your own.
Phoebe
Phoebe 2014년 3월 28일
It is because I use the code to model multiple particles but I have to perform a calculation using a first. It is a particle pusher simulation so i use hundreds of particles but i have to calculate with the value for a first which is an x coordinate. What do you mean by no time compute?

댓글을 달려면 로그인하십시오.

답변 (1개)

Image Analyst
Image Analyst 2014년 3월 28일
Try
for np = 1 : particleno
%Run Function to calculate guiding centre for diagnostic test
[a, b, gcx(np)]=guidingcentrefun(x1(np), x2(np), x3(np), y1(np), y2(np), y3(np));
end
where you're adding an index, np, to the variable. The gcx in the main program is a separate variable from that in the called function, so it's okay to do it like this.
  댓글 수: 4
Phoebe
Phoebe 2014년 3월 28일
Yours works great! I will attach my code and the relevant function files to let it run if you can see what it happening to stop it working? please?
The stuff that is relevant is probably on lines 80 to 85 ??
This would be SO much help!
Phoebe
Phoebe 2014년 3월 28일
Sorry to clarify ParticlePusherNEW is the main script file all others are Function files so the line reference 80-85 is for ParticlePusherNEW

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by