MatLAB fsolve calculation - matlab

have a problem with MatLAB
How can I view the intermediate calculations for a given function? not only for the desired variable, but also for the equations included in the system for fsolve.
fsolve first eq second eq fsolve system
function solve_syst ()
a_init = -2;
c = 6;
d = 8;
for i = 1:7
b = i + 4;
syst_eq = #(a) syst_3 (a, b, c, d);
a_vih = fsolve (syst_eq, a_init);
a(i) = a_vih
end
first eq
function x1 = syst_1 (a, b) %first eq
x1 = a * 6 + 2 * b;
end
second eq
function x2 = syst_2 (c, d)%second eq
x2 = c * 4 - d * 2;
end
system for solve
function [prov, x1, x2] = syst_3 (a, b, c, d) %system for fsolve
x1 = syst_1 (a, b)
x2 = syst_2 (c, d)
prov = x1 - x2;
end

You may step through your program using the Matlab debugger. Just set a breakpoint in your code where you want to see the intermediate results.
You can view the current content of your variables either in the workspace, or by typing their name in the console.
You can also navigate step by step onwards from your breakpoint through your code as explained here.

Related

How do I call a function handle with an vector rather than a list of arguments?

I was wondering how to call a function handle with an vector of inputs - rather than the list of aguments.
So if I have a function handle which is defined: (I guess it will be clear that I am working on fitting functions here)
fitFunctionHandle = #(a1, wG1, x1,a2, wG2, x2, c, x)(FitGaussianFn(x, [a1, 0, wG1, x1]) +FitGaussianFn(x, [a2, 0, wG2, x2]) + c
And I have an vector of inputs to hand to it
a1Init = 1
wG1Init = 2
x1Init = 3
a2Init = 4
wG2Init = 5
x2Init = 6
a2Init = 7
x = 8
%startPoint = [a1Init, wG1, x1,a2, wG2, x2, c]
inputArray = [a1Init, wG1, x1,a2, wG2, x2, c, x]
How do I call it with the vector startPoint as the input. If I try (for example)
>> fitFunctionHandle(inputArray)
I get an error:
Not enough input arguments.
That makes sense since it is expecting 8 inputs rather than a 8 element vector - but I'm wondering whether there is a way of calling it like that. I think for example in python you can convert an array to a list or something.
I'm aware that I could simplify things in this example case where I have a list of known inputs so I could just do:
fitFunctionHandle(a1Init, wG1, x1,a2, wG2, x2, c, x)
or
fitFunctionHandle(inputArray(1),inputArray(2),inputArray(3,inputArray(4), inputArray(5), inputArray(6),inputArray(7),inputArray(8))
BUT! I guess I'm trying to allow for expansion where I don't know how many arguments there would be.
For bonus points (again since I'm working on fitting functions) - It would be cool to know how to call it when x is a vector as well. I suspect that I would be doing something like
arrayfunc(fitFunctionHandle([startPoint,x]), xVector)
Thanks in advance for your help.
If I understand correctly, you are looking for a way to pass a variable number of input arguments to an anonymous function.
Take a look at this example:
anonymousFunction = #(varargin)(image(varargin{:}));
x = [5 8];
y = [3 6];
C = [0 2 4 6; 8 10 12 14; 16 18 20 22];
input = {C};
anonymousFunction(input{:});
input = {x,y,C};
anonymousFunction(input{:});
You can see in the example that a variable number of inputs is used for calling the anonymous function. Of course, this depends on the actual function that is using the arguments (in this case, image)
Regarding the varargin, the general idea was already presented in UnbearableLightness' answer. I just wanted to address your specific case.
It seems, you want to have some dynamic linear combinations of your FitGaussianFn calls with varying parameters, resulting in some complex function handle. For variable amount of terms, I would recommend to write a separate function to generate the final function handle. I made up a small toy example.
That's my fitFunctionHandle.m function:
function h = fitFunctionHandle(varargin)
n = numel(varargin);
if (n < 2)
error('Not enough input arguments.');
end
% Last input argument is considered t
t = varargin{end};
h = #(x) 0;
for iArg = 1:n-1
a = varargin{iArg}(1);
b = varargin{iArg}(2);
h = #(x) h(x) + (a*sin(b*x+t));
end
end
Each input argument consists of two parameters, that form a sin term. An additional shift parameter t is always passed at last.
And, here's some test script:
% Set up parameters
a1 = 1; b1 = 1;
a2 = 2; b2 = 2;
a3 = 3; b3 = 3;
t = pi/16;
% Evaluation point
x = pi/4;
% Compare results with explicit function calls
h = fitFunctionHandle([a1, b1], t);
res = h(x)
cmp = sin(x+t)
h = fitFunctionHandle([a1, b1], [a2, b2], t);
res = h(x)
cmp = sin(x+t) + 2*sin(2*x+t)
h = fitFunctionHandle([a1, b1], [a2, b2], [a3, b3], t);
res = h(x)
cmp = sin(x+t) + 2*sin(2*x+t) + 3*sin(3*x+t)
You see, it doesn't matter, how many parameter vectors are passed.
The output reveals, that the passed function handles are correct:
res = 0.83147
cmp = 0.83147
res = 2.7930
cmp = 2.7930
res = 4.4598
cmp = 4.4598
Regarding your last question, let's see this test:
% Set up array of t parameters, and get set of function handles
t = [pi/16, pi/8, pi/4];
hh = arrayfun(#(x) fitFunctionHandle([a1, b1], x), t, 'UniformOutput', false);
% Compare results with explicit function calls
for iH = 1:numel(hh)
h = hh{iH};
res = h(x)
cmp = sin(x + t(iH))
end
Here, the t, which is used for all sin terms in fitFunctionHanle, is a vector. The arrayfun call will return a cell array of function handles. Again, the output shows comparable results:
res = 0.83147
cmp = 0.83147
res = 0.92388
cmp = 0.92388
res = 1
cmp = 1
Hope that helps!

Performance of `eval` compared to `str2func` to evalulate a function from a string

eval and str2func are both able to evaluate a function represented by a string, f.e. f='a^x+exp(b)+sin(c*x)+d':
Using eval:
y = eval(f)
or (suggested by rahnema1)
fHandle = eval(['#(x, a, b, c, d) ' f]);
y = fHandle(x, a, b, c, d);
Using str2func:
fHandle = str2func(['#(x, a, b, c, d) ' f]);
y = fHandle(x, a, b, c, d);
Which of both methods has the best performance?
Remarks
Note that this benchmark is inspired on this question.
Note that I'm aware that using eval and str2func is often bad practice[1][2] (as mentioned in the comments).
Short answer: use str2func.
Benchmark
The benchmark will evaluate the function for N different values of x.
f='a^x+exp(b)+sin(c*x)+d';
Ns = linspace(1, 1000, 20);
timeEval = zeros(size(Ns));
timeEvalHandle = zeros(size(Ns));
timeStr2func = zeros(size(Ns));
for i=1:length(Ns)
N = Ns(i);
timeEval(i) = timeit(#() useEval(f, N));
timeEvalHandle(i) = timeit(#() useEvalHandle(f, N));
timeStr2func(i) = timeit(#() useStr2func(f, N));
end
figure
plot(Ns, timeEval, 'DisplayName', 'time eval');
hold on
plot(Ns, timeEvalHandle, 'DisplayName', 'time eval');
hold on
plot(Ns, timeStr2func, 'DisplayName', 'time str2func');
legend show
xlabel('N');
figure
plot(Ns, timeEval./timeStr2func, 'DisplayName', 'time_{eval}/time_{str2func}');
hold on
plot(Ns, timeEvalHandle./timeStr2func, 'DisplayName', 'time_{eval handle}/time_{str2func}');
legend show
xlabel('N');
figure
plot(Ns, timeEvalHandle./timeStr2func);
ylabel('time_{eval handle}/time_{str2func}')
xlabel('N');
function y = useEval(f, N)
a = 1; b = 2; c = 3; d = 4;
for x=1:N
y = eval(f);
end
end
function y = useEvalHandle(f, N)
a = 1; b = 2; c = 3; d = 4;
fHandle = eval(['#(x, a, b, c, d) ' f]);
for x=1:N
y = fHandle(x, a, b, c, d);
end
end
function y = useStr2func(f, N)
a = 1; b = 2; c = 3; d = 4;
fHandle = str2func(['#(x, a, b, c, d) ' f]);
for x=1:N
y = fHandle(x, a, b, c, d);
end
end
str2func vs eval (without function handle): The results show that even for evaluating the function once, it is around 50% faster to use str2func than eval (without function handle). For a large number of evaluations, str2func may be around 100x faster (depending on the function you are evaluating).
str2func vs eval (with function handle): eval is around 100% slower than str2func for a single evaluation, but becomes are almost equally fast for a large number of evaluations (eval is ~5% slower).
eval with and without function handle: Note that for a single evaluation creating a function handle with eval is ~50% slower than evaluating it directly.
Conclusion: str2func is always faster than eval.
In the following benchmark a collection of function handles is created and performance of eval is compare to that of str2func to evaluate strings. The functions are combination of variables with +- signs.
n = 16;
op=char((dec2bin(0:2^n-1)-48)*2+43);
vars= 'a':'z';
v = vars(1:n+1);
s(1:2^n,1:2:2*n+1)=repmat(v,2^n,1);
s(:,2:2:end)=op;
h=repmat(['#(' sprintf('%c,',v(1:end-1)) v(end) ')'],2^n,1);
farray=[h,s];
tic
for k = 1:2^n
f = eval(farray(k,:));
end
toc
tic
for k = 1:2^n
f = str2func(farray(k,:));
end
toc
Result in Octave:
There is no difference between two methods.

Optimization with Lagrange multipliers

I have an optimization problem which I have to solve using Lagrange multipliers.
I have the following code:
clear all
clc
p = #(r,sig) (r./sig.^2) .* exp(-r.^2/2*(sig.^2));
sig = 1;
Pa = 1;
Pc = 20;
a = 0.5;
b = 1;
phi = #(h_AB,h_AC,h_CB) abs(h_AB).^2 +((b .* a.^2*abs(h_AC).^2 .* abs(h_CB).^2)
.* Pc ./ (1 + b * a .^2 .* abs(h_CB) .^2 * Pc));
f1 = #(h_AB,h_AC,h_CB) .5*log((1+phi(h_AB,h_AC,h_CB)*Pa)/(1+abs(h_AC).^2*Pa));
f2 = #(h_AB,h_AC,h_CB) f1(h_AB,h_AC,h_CB) .* p(h_AB,sig) .* p(h_AC,sig).* (h_CB,sig);
q = integral3(f2,-inf,inf,-inf,inf,-inf,inf);
The constant b that I have defined should be a variable (q be a function of b), so that I would find e = jacobian(q,[b]); in the next step.
The problem is that I can't define b as symbolic while other functions are not symbolic and when I define all the functions in symbolic form, I get out of memory solving e = jacobian(q,[b]);

Not enough input arguments error in matlab

This is my matlab code , I got Not enough input argument error in line 2 and i don't know how to fix it. Anyhelp ? Thanks in advance .
function [] = Integr1( F,a,b )
i = ((b - a)/500);
x = a;k = 0; n = 0;
while x <= b
F1 = F(x);
x = x + i;
F2 = F(x);
m = ((F1+F2)*i)/2;
k = k +m;
end
k
x = a; e = 0; o = 0;
while x <= (b - 2*i)
x = x + i;
e = e + F(x);
x = x + i;
o = o + F(x);
end
n = (i/3)*(F(a) + F(b) + 2*o + 4*e)
This code performs integration by the trapezoidal rule. The last line of code gave it away. Please do not just push the Play button in your MATLAB editor. Don't even think about it, and ignore that it's there. Instead, go into your Command Prompt, and you need to define the inputs that go into this function. These inputs are:
F: A function you want to integrate:
a: The starting x point
b: The ending x point
BTW, your function will not do anything once you run it. You probably want to return the integral result, and so you need to modify the first line of your code to this:
function n = Integr1( F,a,b )
The last line of code assigns n to be the area under the curve, and that's what you want to return.
Now, let's define your parameters. A simple example for F is a linear function... something like:
F = #(x) 2*x + 3;
This defines a function y = 2*x + 3. Next define the starting and ending points:
a = 1; b = 4;
I made them 1 and 4 respectively. Now you can call the code:
out = Integr1(F, a, b);
out should contain the integral of y = 2*x + 3 from x = 1 to x = 4.

Symbolic function input non-symbolic parameters

I need to define a function that returns a symbolic matrix (sym). It takes 4 input parameters- 2 symbolic matrices, and 2 integers. How do I do that?
This is what I was trying to do-
%my function
function F = matrix(F, F4, i, j)
...
F=...;
end
%calling it in a different file
syms M1;
M1 = ...;
syms M2;
M2 = ...;
syms M3;
M3 = matrix(M1,M2,1,2);
I have done this easy test;
function L = test(A,B,c,d)
syms tmp1 tmp2
tmp1 = c;
tmp2 = d;
L = tmp1*A + tmp2*B;
end
where A and B are already symbolic matrices.