Is it possible for a formula to have a character array input?

Hi, I am trying to have the equation defining a function to vary depending on conditions. I want to know if its possible to add character arrays within the function definition.
Nc=3;
Np=2*Nc;
C=zeros(Np,1);
r=char('k(1)*C(1)*C(3)');
function dCdz = odes(z,C)
for i=1:2:Np
dCdz(i)= C(i+1);
dCdz(i+1)= Pe*(C(i+1)+r(1));
end
end
However, the function does not evaluate as I would expect. Is there a correct way to write this?
Thanks

5 commentaires

You forgot to mention, what you expect and what you get instead. If all we see is the code, we cannot know, what you expect.
Stephen23
Stephen23 le 26 Fév 2018
Modifié(e) : Stephen23 le 26 Fév 2018
@Amy Craig: if you define r as some kind of function k(1)*C(1)*C(3), what does it mean when you try to call it r(1) ? What would r(2) be? Or r(99) ? It is not clear from your example what you want to occur.
Currently you define a character vector r
r = char('k(1)*C(1)*C(3)');
in which the char is totally superfluous, because '' already defines a char vector, so all you needed to write was:
r = 'k(1)*C(1)*C(3)';
Then in your code you use indexing r(1) to get the first element, which happens to be the character 'k'. You then add this character 'k' to some other value, which MATLAB interprets as adding the character code. You can find the character code for 'k' very easily:
>> double('k')
ans = 107
or simply by using any unary or binary arithmetic operator on it, e.g.:
>> +'k'
ans = 107
So that line of your code is simply equivalent to
dCdz(i+1)= Pe*(C(i+1)+107);
"Is there a correct way to write this?"
To write what? What are you trying to do?
@Amy Craig: use str2func.
I am unsure how to use this as the string does not constitute the entire function but only part of it.
Therefore I get the error:
Warning: The input to STR2FUNC "k(1)*t*C(1)*C(3)" is not a valid function name. This will generate an error in a future release.
Undefined operator '*' for input arguments of type 'function_handle'.
Stephen23
Stephen23 le 26 Fév 2018
Modifié(e) : Stephen23 le 26 Fév 2018
@Amy Craig: the str2func documentation at the link that I gave you explains that to define an anonymous function (i.e. not one saved as an Mfile) you will need to prefix the string with @(...), defining any variables you need to call that function with. Note that the documentation also states " Workspace variables are not available to the str2func function. Therefore, include values in the character vector that are necessary to evaluate the expression and that are not defined as function inputs".
So you would need something like this:
fun = str2func('@(k,t,C)k(1)*t*C(1)*C(3)')
and call it with the appropriate input arguments:
fun(k,t,C)

Connectez-vous pour commenter.

Réponses (1)

Jan
Jan le 26 Fév 2018
Modifié(e) : Jan le 26 Fév 2018
A formula can have a char vector as input, but it is evaluated as char vector. For
r = char('k(1)*C(1)*C(3)');
the values of r(1) is 'k'. This is converted to its ASCII value, such that the double 107 is used. This is most likely not, what you want. But what do you want exactly? What is "k" and what do you expect as result of "r(1)"?
Maybe you want an anonymous function:
r = @(k, C) k(1)*C(1)*C(3);
% Needs to be a nested function:
function dCdz = odes(z,C)
dCdz = zeros(1, Np); % Pre-allocate!
for i=1:2:Np
dCdz(i) = C(i+1);
dCdz(i+1)= Pe*(C(i+1) + r(1, C));
end
end

Catégories

En savoir plus sur Functions dans Centre d'aide et File Exchange

Question posée :

le 26 Fév 2018

Modifié(e) :

le 26 Fév 2018

Community Treasure Hunt

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

Start Hunting!

Translated by