Why is this coding showing that? What's the problem here?

2 visualizzazioni (ultimi 30 giorni)

Risposte (1)

Steven Lord
Steven Lord il 22 Feb 2022
The main problem with your line 10 is that square brackets are not used for calling functions. They are used for concatenating arrays together. Therefore derivative[<stuff>] is not valid syntax in MATLAB.
A second problem is that you're using eval. My advice is for you to forget that function exists, at least for now. Using it can prevent MATLAB from optimizing your code as effectively as it could and also generally leads to hard-to-understand code.
Third, there is no command in MATLAB to take the derivative of a function handle and return it in closed form. You could create F1 and F2 as symbolic expressions if you have Symbolic Math Toolbox and then diff those symbolic expressions:
syms x
F = sin(x) + cos(x)
F = 
dF = diff(F, x)
dF = 
Or if you need to solve this numerically use one of the techniques on this Wikipedia page.
Fourth, in your second and third for loops you're using the expression nl(x). Unless nl is a function you haven't showed us this won't work because you cleared the workspace (on line 2, which may not be necessary) and so there can't be a variable named nl in the workspace since none of the code you show defines such a variable.
A fifth minor issue is your last line. Generally there are few situations where you should use the inv function. If you want to solve a system of linear equations whose coefficient matrix you know, use the backslash operator (\) instead of inv.

Community Treasure Hunt

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

Start Hunting!

Translated by