Multiple errors with derivative calculator code

disp('This calculates a polynomial derivative at a specific point')
x = sym ('x');
g = input('What is your polynomial function?','s');
f() = str2func(['@(x)', 'g']);
a = input('What value would you like to derive it at?');
n = limit ((f(x) - f(a))/(x-a),a);
fprintf('The derivative of %s \n', f(x))
fprintf('at point %f is %f',n)
I get an error message here:
Error: File: DerivativeCalculator.m Line: 5 Column: 2
An indexing expression on the left side of an assignment must have at least one subscript.
What are the problems with my code?

 채택된 답변

John D'Errico
John D'Errico 2020년 9월 6일
편집: John D'Errico 2020년 9월 6일

1 개 추천

This is not valid syntax in MATLAB. It may be so in some other language. But not so here.
f() = str2func(['@(x)', 'g']);
As you can see,
g = 'x^2';
f() = str2func(['@(x)', 'g']);
f() = str2func(['@(x)', 'g']);
Error: An indexing expression on the left side of an assignment must have at least one subscript.
The error message tells you MATLAB did not like the f() on the left side of the assignment. The assignment operator in MATLAB is the = sign. To the left of that, MATLAB saw f(), but no index inside the ().
The answer is simple (besides learning MATLAB more completely) is to just write it as:
f = str2func(['@(x)', 'g']);
In MATLAB, f is a variable, that just happens to contain a function handle.
whos f
Name Size Bytes Class Attributes
f 1x1 32 function_handle
You assign something into a variable like you assign any other variable.

댓글 수: 4

Thanks. I then get a problem with the limit itself.
disp('This calculates a polynomial derivative at a specific point')
x = sym ('x');
g = input('What is your polynomial function?','s');
f = str2func(['@(x)', 'g']);
a = input('What value would you like to derive it at?');
n = limit ((f(x) - f(a))/(x-a),a);
fprintf('The derivative of %s \n', f(x))
fprintf('at point %f is %f',a,n)
Error in DerivativeCalculator (line 9)
n = limit ((f(x) - f(a))/(x-a),a);
The limit cannot be calculated for some reason.
And that is because you did not use str2func properly.
What is g? (A string.) For example...
>> g = input('What is your polynomial function?','s');
What is your polynomial function?x^2
>> g
g =
'x^2'
Now, when you tried to use str2func with g, what did you do?
>> f = str2func(['@(x)', 'g']);
>> f
f =
function_handle with value:
@(x)g
WRONG. How did you use g? You are confusing the variable name g, with a string 'g'. What the variable g contains is not the character string 'g'. And just because the variable itself contains a string, does not mean you will access that character string as 'g'.
Instead, you need to access what the variable g contains. Think of a variable as a container. Thus, what does g contain? At least in this case, g holds a character string form of the expression:
>> g
g =
'x^2'
And now if we use g properly, we see:
>> f = str2func(['@(x) ', g]);
>> f
f =
function_handle with value:
@(x) x^2
>> f(2)
ans =
4
Now we see we can evaluate f at any point x. I perhaps should have written g as x.^2, in which case the function would be vectorized properly. But that is probably grist for your next homework assignment.
>> syms x
>> a = 2;
>> limit ((f(x) - f(a))/(x-a),a)
ans =
4
Thank you for your insight! This isn't for a homework assignment. I'm just starting out on MATHLAB with a school license and seeing what I can do with it. I'll look deeper at my code.
Just think about the difference between the name of a variable and what it contains.
Anyway, here is another way you could have performed the differentiation. You used the definition of a derivative as a limit there. But you could just have used diff.
>> g
g =
'x^2'
>> gsym = str2sym(g)
gsym =
x^2
str2sym converts the string into a symbolic variable. So gsym contains essentially the same expression as g, but here it is stored in a different form. whos tells us this fact:
>> whos g gsym
Name Size Bytes Class Attributes
g 1x3 6 char
gsym 1x1 8 sym
But now we can use symbolic tools on gsym.
>> gprime = diff(gsym,x)
gprime =
2*x
And finally we can turn gprime into a function we can evaluate directly.
f = matlabFunction(gprime)
f =
function_handle with value:
@(x)x.*2.0

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

질문:

2020년 9월 6일

편집:

2020년 9월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by