Equation implementation - different to what I expect

Hi,
What is the equation that this piece of code plots?
θ = -pi:0.1:pi
%Below is f(θ)
f = get_f()
%Set Ax = 1
Ax = 1;
%Implement equation
θnew = θ - Ax * f
plot(θnew, f) %plots f(θ) against θnew
I thought it was plotting f(θ - Ax*f(θ)), but upon plotting this directly I found it was different to the code snippet.
Thanks for your input.

1 commentaire

You have not given us enough information as to what get_f() does.

Connectez-vous pour commenter.

 Réponse acceptée

In your statement
θnew = θ - Ax * f
then to avoid problems with that line, f must be a scalar, or a vector the same size as theta, or f must be a function handle to a function that takes no arguments and returns a scalar or a vector the same size as theta. In any of those situations, thetanew would become a vector the same length as theta.
Your comments imply that f is a vector representing the values of f(theta) for the theta you assigned, or else f is a function handle to a function returning a vector the same size as theta. We cannot tell which from what you wrote.
Now you have
plot(θnew, f)
as the next line. f has not changed since the previous line, so if f in the previous line was a vector corresponding to f(theta), then it must be that same numeric vector in the current line. But in your "y" axis, you want f(thetanew) and instead you have the previously-calculated f(theta). If instead f is a function handle to a function that takes no arguments, then where it appears in the plot line it would be a function with the same absent arguments, and that is not going to be f(thetanew)
Possibly you are thinking that when you write
theta - Ax * f
that MATLAB is to "know" that you mean
theta - Ax * f(theta)
and possibly you likewise are thinking in
plot(thetanew, f)
that MATLAB is to "know" that you mean
plot(thetanew, f(thetanew))
MATLAB does not do that, however. MATLAB functions must be called with the argument they are to be applied against. MATLAB functions do not deduce their arguments by context.

Plus de réponses (0)

Catégories

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

Community Treasure Hunt

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

Start Hunting!

Translated by