Is my code set up correctly? (Vectors)

I'm struggling with this and have no idea if my code is correct or where it's gone wrong.
Here's what the question wants:
The formula is:
where:
N = The concentration of solute at position x and time t; No = The initial concentration of solute at x = 0 D = The (so called) diffusion coefficient
I need to write a matlab program where x is a vector with values from 0 to 10. And then "compute a concentration vector N, given the following values for variables appearing in the Fick’s Law approximation, above, that are listed below:" t = 4.5; D = 3.0; and No = 19.0
Here's the code I wrote but I'm really at a loss with this.
x = 0:10;
t = 4.5;
D = 3.0;
No = 19;
for n = 0:10;
N = No * [1-2.*(x/2*sqrt(D*t*pi))]
end

回答(1 个)

It seems to be correct. However you do not need the loop, since the ‘N’ calculation is not a function of ‘n’.
So simply do:
x = 0:10;
t = 4.5;
D = 3.0;
No = 19;
N = No * (1-2.*(x/(2*sqrt(D*t*pi))))
And you need an extra set of parentheses around ‘(2*sqrt(D*t*pi))’.
Also, use parentheses rather than square brackets, since you are not defining a vector of specific elements.

类别

在 帮助中心 和 File Exchange 中查找有关 Get Started with MATLAB 的更多信息

标签

提问:

2018-9-29

Community Treasure Hunt

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

Start Hunting!

Translated by