Converts a string into a function to plot

So I have mathematic functions like sin(x), cos(x) etc. in a string format that I want to convert into a function so I would be able to plot it.
equ='sin(x)';
f= @(x) equ;
plot(x,f)
error message:
Error using plot
Invalid data argument.

2 件のコメント

Stephen23
Stephen23 2020 年 9 月 4 日
編集済み: Stephen23 2020 年 9 月 4 日
"So I have mathematic functions like sin(x), cos(x) etc. in a string format that I want to convert into a function..."
Your example contains no strings or character arrays at all:
equ=sin(x);
f= @(x) equ;
This makes your question difficult to interpret. Do you actually have a character vector? E.g.:
str = 'sin';
Please show the actual definition of your input data.
iseinas
iseinas 2020 年 9 月 4 日
Sorry, it seems like I missed out on '' when I changed my code to make it simple here.

サインインしてコメントする。

 採用された回答

Stephen23
Stephen23 2020 年 9 月 4 日
編集済み: Stephen23 2020 年 9 月 4 日

0 投票

>> equ = 'sin(x)';
>> fun = str2func(sprintf('@(x)%s',equ));
>> fun(pi/2)
ans =
1

6 件のコメント

iseinas
iseinas 2020 年 9 月 4 日
Thank you so much. What does this part mean though out of curiosity --> @(x)%s?
iseinas
iseinas 2020 年 9 月 4 日
So I tried out your code, it does work when you only use it like you did fun(pi/2) but it somehow doesn't let me plot it.
Stephen23
Stephen23 2020 年 9 月 4 日
編集済み: Stephen23 2020 年 9 月 4 日
"What does this part mean though out of curiosity --> @(x)%s?"
If you had provided a string which was simply the name of a function, e,g.:
>> equ = 'sin';
then it would be possible to just call str2func on it directly, its output would be a handle to that function:
>> fun = str2func(equ);
>> fun(pi/2)
ans = 1
But what you gave us is a formula involving x (that formula can be arbitrarily complex, but it is clearly defined with the independent variable x). Therefore we have to create an anonymous function (which is your entire formula) defined in terms of your independent variable (x) and return a handle to that.
So the sprintf lets us define that anonymous function. If you print it clearly looks just like an anonymous function definition (except that it is actually a character vector):
>> equ = 'sin(x)';
>> sprintf('@(x)%s',equ)
ans = @(x)sin(x)
Then str2func converts that character vector into a handle to an actual anonymous function.
iseinas
iseinas 2020 年 9 月 4 日
Thanks I get that part now, but still cannot plot the graph. I still get the same error when try to plot it.
Stephen23
Stephen23 2020 年 9 月 4 日
編集済み: Stephen23 2020 年 9 月 4 日
"it does work when you only use it like you did fun(pi/2) but it somehow doesn't let me plot it."
I have no problems plotting it. As you did not show your code I have no way to debug what you tried.
"...but still cannot plot the graph. I still get the same error when try to plot it."
Not only is your code a secret, so is the error message that you get. No information -> no debugging help.
Well, it works for me. Lets try it together using two different plotting commands:
>> equ = 'sin(x)';
>> fun = str2func(sprintf('@(x)%s',equ));
>> X = linspace(-2*pi,2*pi,101);
>> Y = fun(X);
>> plot(X,Y)
>> ezplot(fun)
iseinas
iseinas 2020 年 9 月 4 日
I found out I can't directly plot fun, but gotta go through fun(x). Thanks a lot! You really helped me a lot there :)

サインインしてコメントする。

その他の回答 (1 件)

KSSV
KSSV 2020 年 9 月 4 日

0 投票

equ=sin(x);
f= @(x) equ;
x = linspace(0,2*pi) ;
plot(x,f(x))

1 件のコメント

iseinas
iseinas 2020 年 9 月 4 日
Forgot to include that sin(x) is a string-
equ = 'sin(x)'

サインインしてコメントする。

カテゴリ

タグ

質問済み:

2020 年 9 月 4 日

コメント済み:

2020 年 9 月 4 日

Community Treasure Hunt

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

Start Hunting!

Translated by