Can't figure out the mistake here. Gives me an error at line 5 and 16

2 visualizaciones (últimos 30 días)
Gerardo Vega
Gerardo Vega el 16 de Feb. de 2020
Comentada: Walter Roberson el 16 de Feb. de 2020
  5 comentarios
Sindar
Sindar el 16 de Feb. de 2020
But, a first guess: you are trying to define a function of z, but Matlab reads it as indexing, so throws an index error ("
Array indices must be positive integers or logical values"). To define an anonymous function f of Z:
f = @(Z) Z.^4 + 0*Z.^3 - 0.84*Z.^2 - 0.16;
and to return the values for particular z:
fz = f(z);
Walter Roberson
Walter Roberson el 16 de Feb. de 2020
It is a lot easier for people to work with actual code, rather than a picture of code.

Iniciar sesión para comentar.

Respuestas (1)

Walter Roberson
Walter Roberson el 16 de Feb. de 2020
Your z is clearly complex valued numeric, but you are trying to use z as an index into an array named f that you are creating.
You have confused creating a formula and creating an array.
There are two ways to create a formula:
  1. Use the symbolic toolbox, create symbolic variables with sym or syms, use those in an expression, and assign the result to something of the form NAME(variable) or NAME with list of variables if appropriate. For example, syms Z; F(Z) = Z^4+3; would create a symbolic function named F with symbolic variable Z . Later you can pass particular numeric or symbolic values to the function, such as F(1+2i) . This creation of formulas is the only time that a symbolic variable can appear on the left hand side of an assignment
  2. Create an anonymous function using @(variable) or list of variables if appropriate. For example F = @(Z) Z.^4+3; would create an anonymous function assigned to F with dummy parameter name Z. Later you can pass particular numeric or symbolic values to the function, such as F(1+2i) . Notice that in this case, the output, F is a simple variable name; it could also be an individual member of a cell array, or an individual field of a struct . It cannot, however, be something that is indexed with () [with the exception that you can get away with assigning to F(1)] . You cannot use F(Z) = @(something) something -- well except for the obscure possibility that it would happen to work if Z were the scalar value 1.
In all other cases, instead of defining a formula, you should be assigning to an array. The output location is potentially indexed, so F(Z) = something is potentially valid, but only in the case that all of the values are either positive integers (not complex valued), or are logical values.
In context, you should be defining anonymous functions for f and dz, and you should be invoking them on the current z value: z = z - f(z)./dz(z) -- but first you need to vectorize your f and dz, by changing all of the ^ to .^
Also, your dz is wrong. When you took the derivative of -0.84*z^2 you forgot to multiply by 2. Derivative of A*x^2 is 2*A*x not A*x

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by