Info

Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

where is the mistake in this code?

1 Ansicht (letzte 30 Tage)
moji
moji am 5 Sep. 2014
Geschlossen: MATLAB Answer Bot am 20 Aug. 2021
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)

Antworten (3)

Chad Greene
Chad Greene am 5 Sep. 2014
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 Kommentar
moji
moji am 5 Sep. 2014
Bearbeitet: per isakson am 5 Sep. 2014
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 am 5 Sep. 2014
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 am 5 Sep. 2014
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 Kommentare
Star Strider
Star Strider am 5 Sep. 2014
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 am 5 Sep. 2014
Bearbeitet: per isakson am 5 Sep. 2014
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.

Diese Frage ist geschlossen.

Community Treasure Hunt

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

Start Hunting!

Translated by