Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

where is the mistake in this code?

조회 수: 1 (최근 30일)
moji
moji 2014년 9월 5일
마감: MATLAB Answer Bot 2021년 8월 20일
v=0.25;
x= 60;
D=0.01;
kmax=3.1624E-5;
X=1;
Ks=8.9550E-5;
a0=1;
t=1:56;
for tt= 1:1:length(t)
ka=kmax*X/Ks;
B=(((v^2)/(4*D^2) +(ka/D)) ^ 0.5);
C(tt)=(a0/2)*exp(-B*x)*erfc((x-(v^2 +4*ka*D)^0.5*t)/(2*(D*t)^0.5)) + exp(B*x)*erfc((x-(v^2 +4*ka*D)^0.5 *t)/(2*(D*t)^0.5));
end
plot(tt,C)

답변 (3개)

Chad Greene
Chad Greene 2014년 9월 5일
I'm not sure if this is a coincidence, but you've bolded two of the mistakes. There's an 0.5 t and a 2(D*t). Did you mean 0.5*t and a 2*(D*t)? Or perhaps plus or minus or divided by or to the power of?
Another thing: put a . before the ^ signs to make them .^ if you're doing element-wise raising to a power.
And finally, should the plot be plot(t,C)?
  댓글 수: 1
moji
moji 2014년 9월 5일
편집: per isakson 2014년 9월 5일
Is this correct? I still get this error:
??? Error using ==> mpower Inputs must be a scalar and a square matrix.
Error in ==> analyticalsolution at 14
C(tt)=(a0/2)*exp(-B*x)*erfc((x-((v.^2 +4*ka*D).^0.5)*t)/(2*(D*t)^0.5)) + exp(B*x)*erfc((x-((v.^2
+4*ka*D).^0.5)*t)/(2*(D*tt).^0.5));
this is the code with spaces:
for tt= 1:1:length(t)
ka=kmax*X/Ks;
B=(((v^2)/(4*D.^2) +(ka/D)) .^ 0.5);
C(tt)=(a0/2) * exp(-B*x) * erfc((x-((v.^2 +4*ka*D).^0.5)*t) / (2*(D*t)^0.5)) + exp(B*x) * erfc((x-((v.^2 +4*ka*D).^0.5)*t) / (2*(D*tt).^0.5));
end
plot(tt,C)

per isakson
per isakson 2014년 9월 5일
Don't write longer lines than you can handle. Introduce temporary variables like
meaningful_name = (2*(D*t)^0.5);
that will make the error messages easier to interpret.

Star Strider
Star Strider 2014년 9월 5일
First, consider whether you should replace t by tt in your C(tt) assignment. Otherwise, C(tt) never changes.
Second, even with that change, if you break C(tt) down into its components, the problem becomes quickly apparent. Add this line before C(tt):
Cv(tt,:) = [exp(-B*x) erfc((x-(v^2 +4*ka*D)^0.5*tt)/(2*(D*tt)^0.5)) exp(B*x) erfc((x-(v^2 +4*ka*D)^0.5 *tt)/(2*(D*tt)^0.5))];
For all values of tt:
Cv(tt,:) = [0 0 Inf 0]
so: C(tt) = NaN everywhere.
  댓글 수: 3
Star Strider
Star Strider 2014년 9월 5일
I actually did not suggest any parts for you to change — you have to figure that out, since I do not know what you are doing.
I only suggested a way for you to figure out what is wrong with your code.
The arguments to erfc are going to be zero as you calculate them, so you first need to understand exactly what you are doing with erfc. Once you have that problem solved, you can use logarithms if necessary, replacing exp(±B*x) with (±B*x) for your interim calculations, Then take antilogs at the end.
You may have problems with the units of your variables, so look closely at those to be sure all the units of your variables are correct and the magnitudes are correct.
per isakson
per isakson 2014년 9월 5일
편집: per isakson 2014년 9월 5일
Set Stop on Errors:
dbstop on errors
Run the function. At line 17 select f(tt,:), right click, chose Evaluate Selection and you will find that it is not a &nbsp<1x4 double>.
Possibly, you have to pre-allocate f with something like
f = nan( length(t), 4 );
nan is good because it will make a fuzz if not all element are assigned values.

이 질문은 마감되었습니다.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by