필터 지우기
필터 지우기

How can I do the same calculation multiple times for specified values of a variable

조회 수: 9 (최근 30일)
I am calculating compositonal profiles with a very simple version of the diffusion equation.
I want to do the same calculation several times and store the values in the workspace (ideally as mutliple columns for a single variable).
I have figured out the followng way to do it - but I am sure that there is a more elegant solution to my problem:
x = 0:1:100;
Co = 22;
t1 = 30000;
t2 = 60000;
t3 = 120000;
D = 0.001;
C1 = Co*erf(x/(D*t1).^0.5);
C2 = Co*erf(x/(D*t2).^0.5)
C3 = Co*erf(x/(D*t3).^0.5)
plot(x,C1, x, C2, x,C3)
Thanks
Cliff

채택된 답변

Star Strider
Star Strider 2020년 2월 14일
Thios does the same as yoiur code, using a loop, and a simplified plot call:
x = 0:1:100;
Co = 22;
t1 = 30000;
t2 = 60000;
t3 = 120000;
t = [t1 t2 t3];
D = 0.001;
for k = 1:numel(t)
C(k,:) = Co*erf(x/(D*t(k)).^0.5);
end
figure
plot(x,C)
grid
It is may be a bit more efficient. I did not assess that specifically.
  댓글 수: 2
Cliff Shaw
Cliff Shaw 2020년 2월 14일
Works nicely and is more elegant than my solution
Thanks
Star Strider
Star Strider 2020년 2월 14일
As always, my pleasure!
No worries. MATLAB has a lot of useful characteristics that take a while to learn about.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Title에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by