Function runs in one script but not another ?

2 views (last 30 days)
Niamh Price
Niamh Price on 29 Apr 2021
Answered: Star Strider on 29 Apr 2021
working on a function about concentration, works fine in its own script conc.m but when i cpoy and paste to another script where i need it it doesn't plot a graph.
heres the function:
function[] = conc()
v = 0.00001 ;
D = 0.0000001 ;
t = 86400 ;
X = 0 : 0.01 : 2 ;
C = (1/(sqrt(4*pi*D*t)))*exp((-(X-v*t).^(2))/(4*D)) ;
plot (X, C, 'r-')
title ('Concentration of substance with respect to distance from source')
xlabel ('distance from source, m')
ylabel ('concentration, mol/l')
end
any help much appreciated

Answers (1)

Star Strider
Star Strider on 29 Apr 2021
Vectorise its calculation, then call it —
conc
function[] = conc()
v = 0.00001 ;
D = 0.0000001 ;
t = 86400 ;
X = 0 : 0.01 : 2 ;
C = (1/(sqrt(4*pi*D*t))).*exp((-(X-v.*t).^(2))/(4*D)) ;
plot (X, C, 'r-')
title ('Concentration of substance with respect to distance from source')
xlabel ('distance from source, m')
ylabel ('concentration, mol/l')
end
See Array vs. Matrix Operations for details.

Community Treasure Hunt

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

Start Hunting!