How to use fzero() to solve polynomial equation in MATLAB? - matlab

I would like to solve the following polynomial numerically for r:
I am trying to use fzero() as follows:
r = (5/(r^2*9))- ((2)/(9*(6-r)^2))
x0 = 10; % some initial point
x = fzero(r,x0)
How can this be done with fzero()?

Input variable and function name should be different
Just change the function name to pol
To use fzero the function pol have to be a function handle
defined via #
pol =#(r) (5/(r^2*9))- ((2)/(9*(6-r)^2))
x0 = 10; % some initial point
x = fzero(pol,x0)
solution
x = 3.6754

It should be noted that, the first argument in fzero() should be "a function handle, inline function, or string containing the name of the function to evaluate", but yours is just an expression, which is not valid.
Besides the approach by #Adam (using function handle), another way is to use anonymous function, i.e.,
x = fzero(#(r) (5/(r^2*9))- ((2)/(9*(6-r)^2)) ,x0)
where
#(r) (5/(r^2*9))- ((2)/(9*(6-r)^2))
is the anonymous function with respect to argument r.
You will get the same result that x = 3.6754.

Related

User Made Matlab Function Fails Once All Variables are Cleared

I originally asked this question yesterday and found the answer myself; however, I used the clear all command in Matlab and now the function throws an error Undefined function or variable 'y'.
I used the code from my answer
function [s1] = L_Analytic3(eqn,t0,h,numstep,y0)
%Differential Equation solver for specific inputs
% eqn is the differential equation
% t0 is start of evaluation interval
% h is stepize
% numstep is the number of steps
% y0 is the initial condition
syms y(x)
cond = y(0) == y0;
A = dsolve(eqn, cond);
s1 = A;
S1 = s1;
for x = t0 : h : h*(numstep)
subs(x);
if x == t0
S1 = subs(s1,x);
else
S1 = [subs(S1), subs(s1,vpa(x))];
end
end
end
and also put L_Analytic3(diff(y) == y,0,0.1,5,1) into the Command Window after entering clear all. I have to run a seperate code
syms y(x)
cond = y(0) == 1;
A = dsolve(diff(y) == y, cond);
before using my function in order for the function to work. Is this just because A,ans,cond,x, and y, are already defined by the 3 line code before using the function? If so, is there a way that I can use the function without having to use that 3 line code first?
When you do L_Analytic3(diff(y) == ...); you do not have variable y defined, so MATLAB complains - it has no way of knowing y is a symbol that will be defined in the function you are calling. You do not require all 3 lines of code. syms y(x) should be enough to define y and lets you use the function call you wanted.
Now, there are 2 easy ways to fix this that I see:
A script (or another function) that has syms y(x), followed by the call to L_Analytic3 the way you are doing it (which now does not need syms y(x), it has been defined already).
Give anonymous equation as the input instead, say #(x) diff(x)==x, and change a line of L_Analytic3 slightly to A = dsolve(eqn(y), cond);
Both ways work fine for this, no idea if 2nd one breaks in more complex cases. I would likely pick 1st version if you are doing symbolic stuff, and 2nd if you would like to have same function call to both numeric and symbolic functions.

MATLAB function handles and parameters

When I type help gmres in MATLAB I get the following example:
n = 21; A = gallery('wilk',n); b = sum(A,2);
tol = 1e-12; maxit = 15;
x1 = gmres(#(x)afun(x,n),b,10,tol,maxit,#(x)mfun(x,n));
where the two functions are:
function y = afun(x,n)
y = [0; x(1:n-1)] + [((n-1)/2:-1:0)'; (1:(n-1)/2)'].*x+[x(2:n); 0];
end
and
function y = mfun(r,n)
y = r ./ [((n-1)/2:-1:1)'; 1; (1:(n-1)/2)'];
end
I tested it and it works great. My question is in both those functions what is the value for x since we never give it one?
Also shouldn't the call to gmres be written like this: (y in the #handle)
x1 = gmres(#(y)afun(x,n),b,10,tol,maxit,#(y)mfun(x,n));
Function handles are one way to parametrize functions in MATLAB. From the documentation page, we find the following example:
b = 2;
c = 3.5;
cubicpoly = #(x) x^3 + b*x + c;
x = fzero(cubicpoly,0)
which results in:
x =
-1.0945
So what's happening here? fzero is a so-called function function, that takes function handles as inputs, and performs operations on them -- in this case, finds the root of the given function. Practically, this means that fzero decides which values for the input argument x to cubicpoly to try in order to find the root. This means the user just provides a function - no need to give the inputs - and fzero will query the function with different values for x to eventually find the root.
The function you ask about, gmres, operates in a similar manner. What this means is that you merely need to provide a function that takes an appropriate number of input arguments, and gmres will take care of calling it with appropriate inputs to produce its output.
Finally, let's consider your suggestion of calling gmres as follows:
x1 = gmres(#(y)afun(x,n),b,10,tol,maxit,#(y)mfun(x,n));
This might work, or then again it might not -- it depends whether you have a variable called x in the workspace of the function eventually calling either afun or mfun. Notice that now the function handles take one input, y, but its value is nowhere used in the expression of the function defined. This means it will not have any effect on the output.
Consider the following example to illustrate what happens:
f = #(y)2*x+1; % define a function handle
f(1) % error! Undefined function or variable 'x'!
% the following this works, and g will now use x from the workspace
x = 42;
g = #(y)2*x+1; % define a function handle that knows about x
g(1)
g(2)
g(3) % ...but the result will be independent of y as it's not used.

Unconstrained nonlinear optimization function

I want to optimize an unconstrained multivariable problem using fminunc function in MATLAB. Here is an example:
Minimize the function f(w)=x'Ax
Create a file myfun.m:
function f = myfun(x)
f = x'*A*x + b'x
Then call fminunc to find a minimum of myfun near x0:
[x,fval] = fminunc(#myfun,x0).
My problem is that in my algorithm, the matrix A and vector b in myfun.m are not fixed, but can be changed over loops, so I cannot type them by hand. How can I pass values to A and b?
There are a few options for passing additional arguments to an objective function. For a simple one like yours, you could just make an anonymous function, which will save the values of A and b when it was created:
A = myMatA();
b = myVecb();
myfun = #(x) x.'*A*x + b.'*x;
[x,fval] = fminunc(myfun,x0); % use no # with an anonymous function
The other two options are global variables (yuck!) and nested functions. A nested function version looks like this:
function [x,fval] = myopt(A,B,x0)
[x,fval] = fminunc(#myfunnested,x0);
function y = myfunnested(x)
y = x.'*A*x + b.'*x;
end
end
But I think you would not use fminunc to solve minimization of x'Ax + b'x...

Why can't I pass a function handle to fsolve, but I can write an equivalent anonymous function in fsolve?

I find this somewhat odd. I am currently writing a simple function to solve a system of equations using fsolve. Here is what I have:
%Variable Declarations
I0 = 10e-12;
n = 1;
Vt = 0.0259;
R = 10e3;
Vs = 3;
%Function 1 (Some may recognize that this is the Shockley Diode Equation, if anyone cares...)
i1 = #(v1)(I0) * (exp((v1)/(n*Vt))-1);
%Function 2
i2 = #(v1) ((Vs-v1)/R);
%This is what I originally tried
h = #(v1) i1(v1)-i2(v1);
fsolve(h(v1), 1)
%After running this, I receive "Undefined function or variable 'v1.'"
% However, if I write
fsolve(#(v1)i1(v1)-i2(v1),1)
%The function works. With the result, I plugged that value into h(v1), and it produces the expected result (very close to 0)
That said, why doesn't matlab allow me to pass a function handle to fsolve?
You want to pass a function handle, which is h, not h(v1). h(v1) itself can't be evaluated because v1 is not defined.
Try fsolve(h, 1)

solving nonlinear equations

I want to solve two nonlinear equations in MATLAB so i did the following:
part of my script
c=[A\u;A\v];
% parts of code are omitted.
x0=[1;1];
sol= fsolve(#myfunc,x0);
the myfunc function is as follows
function F = myfunc(x)
F=[ x(1)*c(1)+x(2)*c(2)+x(1)*x(2)*c(3)+c(4)-ii;
x(1)*c(5)+x(2)*c(6)+x(1)*x(2)*c(7)+c(8)-jj];
end
i have two unknowns x(1) and x(2)
my question is How to pass a values(c,ii,jj) to myfunc in every time i call it?
or how to overcome this error Undefined function or method 'c' for input arguments of type 'double'.
thanks
Edit: The previous answer was bogus and not contributing at all. Hence has been deleted. Here is the right way.
In your main code create a vector of the coefficients c,ii,jj and a dummy function handle f_d
coeffs = [c,ii,jj];
f_d = #(x0) myfunc(x0,coeffs); % f_d considers x0 as variables
sol = fsolve(f_d,x0);
Make your function myfunc capable of taking in 2 variables, x0 and coeffs
function F = myfunc(x, coeffs)
c = coeffs(1:end-2);
ii = coeffs(end-1);
jj = coeffs(end);
F(1) = x(1)*c(1)+x(2)*c(2)+x(1)*x(2)*c(3)+c(4)-ii;
F(2) = x(1)*c(5)+x(2)*c(6)+x(1)*x(2)*c(7)+c(8)-jj;
I think that should solve for x0(1) and x0(2).
Edit: Thank you Eitan_T. Changes have been made above.
There is an alternative option, that I prefer, if a function handle is not what you are looking for.
Say I have this function:
function y = i_have_a_root( x, a )
y = a*x^2;
end
You can pass in your initial guess for x and a value for a by just calling fsolve like so:
a = 5;
x0 = 0;
root = fsolve('i_have_a_root',x0,[],a);
Note: The [] is reserved for fsolve options, which you probably want to use. See the second call to fsolve in the documentation here for information on the options argument.