my function is invalid and fzero can't handle it - octave

I get this error
error: invalid function handle, unable to find function for #0.1*x - sin(2 * x) + 0.25
error: called from
fzero at line 178 column 6
lab6 at line 82 column 3
Part of code where it happens is:
f= '0.1*x - sin(2 * x) + 0.25';
[a, b] = fgraf(f, -3, 3);
fzero(f, [a, b]);
zs1 = ans;
[a, b] = fgraf(f, -3, 3);
zs2 = fzero(f, [a, b]);
[a, b] = fgraf(f, -3, 3);
zs3 = fzero(f, [a, b]);

fgraf doesn’t seem to be a standard MATLAB function, so I can’t be sure, but it looks like it expects a function handle as input. Define f as
f = #(x) 0.1*x - sin(2 * x) + 0.25;

Related

Matlab - Undefined function 'int_f_1' for input arguments of type 'double'

Currently, I am working on a program that integrates x + x^2 + e^x + 2cos(x/2) - 1 with three input variables, a, b, and n. What I need returned is the numerical integral from a to b with n increments. The function also has to return trapezoids for each n as a column vector. Thus, the integral value as a scalar, and a vector of values.
I've gotten to a point where the function int_f_1 is undefined for some reason, and I have no idea why. I thought by nesting that function under the test function, it would help. But it does not, and I don't know why that is. Any suggestions?
function [y] = test_function_1(x);
y = x + x.^2 + exp(x) + 2*cos(x/2) - 1
end
function [int_f, increment] = int_f_1 (a, b, n);
f = #test_function_1;
h = a + b ./ n
increments = h
int_f = integral(h, f)
end

Define a polynomial function in Julia

I want to create the function using Polynomials package in Julia, while c is a Vector and n is the length of the Vector. I can make it in JuMP package but not in Polynomials.
Any idea how to create this function?
Just call Polynomial on this vector c.
julia> c = [1, 2, 3, 4]
4-element Vector{Int64}:
1
2
3
4
julia> p = Polynomial(c)
Polynomial(1 + 2*x + 3*x^2 + 4*x^3)
julia> c = [2, 4, -8, 0, 0, 1];
julia> p = Polynomial(c)
Polynomial(2 + 4*x - 8*x^2 + x^5)
julia> p(2) #evaluate the polynomial at x = 2
10

How to write a function that calculates F(x) if x = integer in sml

I am trying to write a recursive function that takes a list and an integer as a parameter, where the list is the polynomial i.e. (val P = [5.0, 4.0, 0.0, 1.0] = x^3 + 4x - 5) and the integer parameter is x i.e. x = 2 so f(2) = x^3 +4x - 5. Here is what Ive come up so far...
val P = [5.0, 4.0, 0.0, 1.0];
val a = 2;
fun eval(nil, a) = 0.0
| eval(x::xs, a:real) =
x + (eval(xs, a) * a)
eval(P, a);
after running the code it is giving me this error:
stdIn:22.4-24.11 Error: operator is not a function [tycon mismatch]
operator: real
in expression:
(eval (xs,a) * a) eval
the way the recursion is "suppose" to work is P = a + Qx where P is the original list and a is x in x::xs and Q is xs and the x is the integer parameter that was passed.
Any help would be much appreciated. Thanks!!
Ok so I figured it out. Here go...
val P = [~5.0, 4.0, 0.0, 1.0];
val a = 2.0;
fun eval(nil, a) = 0.0
| eval(x::xs, a:real) =
x + (eval(xs, a) * a);
eval(P, a);
Since you answered your own question (good on you!) here's another way to write it:
fun eval ([], _) = 0.0
| eval (x::xs, y) = x + eval (xs, y) * y

Vectorization of function contains the numerical computation of definite integral in octave

I'm trying to fit my data using a formula containing a numerical calculation of definite integrals with infinite limits of integration. For fitting I use octave function leasqr that requires vectorization model function. The following code generates error, that occurs when calling a numerical integration.
nonconformant arguments (op1 is 1x387, op2 is 10x2)
function [fGsAb] = GsAbs (x, p)
Hw = 3108.0 ;
fGsAb = Hw ./ (2.4 .* p(1) .*p(2)) .^2 .* (exp ( - (Hw - p(1) .* x) .^2 ...
./ (2.4 .* p(1) .*p(2)) .^2 ) - exp ( - (Hw + p(1) .* x) .^2 ...
./ (2.4 .* p(1) .*p(2)) .^2 )) ;
endfunction
function [GsDisp] = gauss_disp(x, p)
[GsDisp1, err] = quadgk ( #(z) GsAbs(z, p) ./(z-x), - inf, x - 0.000001 );
[GsDisp2, err] = quadgk ( #(z) GsAbs(z, p) ./(z-x), x + 0.000001 , inf );
GsDisp = GsDisp1 +GsDisp2;
endfunction
h = [200:15:6000];
pin =[1 250];
dd = gauss_disp (h, pin);
If I'm using loop:
for i = 1 : length (h)
dd (i) = gauss_disp (h (i), pin)
endfor
I have no errors, but I cannot use this construction in leasqr. How can I get around this restriction?
Thank you in advance!
Change your gauss_disp.m file from this:
function [GsDisp] = gauss_disp(x, p)
[GsDisp1, err] = quadgk ( #(z) GsAbs(z, p) ./(z-x), - inf, x - 0.000001 );
[GsDisp2, err] = quadgk ( #(z) GsAbs(z, p) ./(z-x), x + 0.000001 , inf );
GsDisp = GsDisp1 +GsDisp2;
endfunction
to this:
function [GsDisp] = gauss_disp(x, p)
GsDisp = arrayfun(#(z) gauss_disp_elementwise(z, p), x)
endfunction
function GsDisp = gauss_disp_elementwise(x, p)
[GsDisp1, err] = quadgk ( #(z) GsAbs(z, p) ./(z-x), - inf, x - 0.000001 );
[GsDisp2, err] = quadgk ( #(z) GsAbs(z, p) ./(z-x), x + 0.000001 , inf );
GsDisp = GsDisp1 +GsDisp2;
endfunction
I.e. make your original function into a helper subfunction that only deals with scalar values, and then use that with arrayfun to get outputs for the entire range of x. This way you can use this in your leasqr function, e.g.:
y = leasqr(h, dd, pin, #gauss_disp);
PS: the arrayfun syntax is just for convenience. you could have easily used a for loop similar to what you used in your question instead.

mathematica Sum of functions

I want to build a function in the following way:
f[x_] := 0;
For[i = 1, i <= n, i++,
g[x_] := 0;
For[k = 1, k <= i, k++,
g ^:= g[#]*(# - X[[k]]) &;
g[x_] = g;
]
f ^:= f[#] + Q[[1, i]]*g[#];
f[x_] = f;
So I get a polynomial
Q_11 * (x-x_1) + Q_12 * (x-x_1) * (x-2x_2) ...
This is the latest version I have, but it does not work. The problem is that I have to add functions (I found some posts where it is done) but I have to save the new function so I can use it again.
Does someone know what to do?
Thanks!
Marius
I cant make head or tail of your code but you can readily construct that result just like this:
qlist = Array[q, {10, 10}];
xlist = Array[x, 10];
poly[n_] := Sum[ qlist[[1, j]] Product[ (x - xlist[[i]] ), {i, j}] , {j, 3}]
poly[3]
q[1, 1] (x - x[1]) + q[1, 2] (x - x[1]) (x - x[2]) +
q[1, 3] (x - x[1]) (x - x[2]) (x - x[3])
If you really need help using UpSetDelayed you should pose a more simple / specific question ( ie without the For loops. )