Explicit polyfit with variables - matlab

I am using polyfit to get the following 4-degree polynomial:
0.5152 -1.0687 0.0269 1.1781 -0.4943
I need this polynomial explicitly, i.e., I need to have the variables in it, too. That is I need it as a symbolic expression, e.g.,
f(q) = 0.5152 q^4 -1.0687 q^3 0.0269 q^2 1.1781 q -0.4943
because my function f(q) is the input of another function g(q). Example: I am having function g as:
g(q) = q^2
and I need f(q) WITH variable q in it so that I can evaluate g at f symbolically. That is, the result should be a SYMBOLIC function g:
g(f(q)) = ( 0.5152 q^4 -1.0687 q^3 0.0269 q^2 1.1781 q -0.4943 )^2
Any ideas how I can represent f(q) from polyfit such that I can use it as symbolic input in g?

Use poly2sym to transform the vector of coefficients into a symbolic polynomial:
>> poly2sym([1 2 3],'x')
ans =
x^2 + 2*x + 3

Related

How to extract a matrix of symbolic functions in Matlab

syms c A(t) v(t)
A(t) =
0
c*sin(tt(t))
c*cos(tt(t))
How I can get X = A(2) = c*sin(tt(t)); (the function at second row)? If I type A(2), the result will be as below (it substitutes a constant for the function, which is not my desire):
>> A(2)
ans =
0
c*sin(tt(2))
c*cos(tt(2))
The problem is that you've defined A as a symbolic function (symfun), not as an array of symbolic expressions. Instead:
syms c A tt(t)
A = [0;
c*sin(tt(t));
c*sin(tt(t))];
Now A(2) will return c*sin(tt(t)).
Alternatively, if you can't change the definition of A(t), you'll need to assign it to an intermediate variable to convert it to an array of symbolic expressions:
syms c A(t) tt(t)
A(t) = [0;
c*sin(tt(t));
c*cos(tt(t))];
B = A(t);
Then, B(2) will return c*sin(tt(t)). You can also use formula to extract the underlying expressions:
B = formula(A):
In matlab you must to use "subs(f)" function to evaluate functions.
First create the function:
syms g(x)
g(x) = x^3;
After that asign the X value:
x=2;
then if you evaluate g using the subs function, the result is the expected value 8, but it is assigned to a symbolic function, gnew. This new symbolic function formally depends on the variable x.
gnew = subs(g)
The function call, g(x), returns the value of g for the current value of x. For example, if you assigned the value 2 to the variable x, then calling g(x) is equivalent to calling g(2)
g2 = g(x)
g2 =
4
g2 = g(2)
g2 =
4

Algorithm to find a value that gives the minimum output for two or equations

Suppose I have two equations with only one variable (free parameter) x and that k1 and k2 are constants. I would like to solve for:
f(x) + k1 = 0
&
g(x) + k2 = 0
...
h(x) + kn = 0
Of course there is no value of x that satisfies all of these equations. I basically would like the value of x that minimizes the output of each of these equations.
'solve' in matlab looks for an exact answer and returns an error, here's an example to demonstrate:
syms x
solution = solve(0.5*(x-k1)/sqrt(2) == 0, 0.5*(x-k2)/sqrt(2) == 0);
You can try using Unconstrained Optimization method such as fminsearch, for example:
h=#(x) x^2;
g=#(x) x^3;
k1=2;
k2=4;
inital_guess=3;
f = #(x) sum(abs([h(x)+k1; g(x)+k2]));
[x,fval] = fminsearch(f,inital_guess)
Note that I represent both eq in matrix form, and the minimization is by looking at the sum of their absolute values.
For the values I entered the value of x that minmize these eq is given by the output x = -1.5874

Plotting function in octave

I have a simple function I wrote:
function f = G(s)
f = 16/(s.^2+3*s+16)
endfunction
I want to plot this transfer function from s = 0 to 4 with a step of 0.01. For some reason I can't get it to work. I am getting nonconformant arguments errors. I am new to octave.
If it is a transfer function, then you want to use the control package to get a Bode plot rather than plotting it as a function, which don't really make sense (s being complex):
>> G = tf(16,[1 3 16])
Transfer function 'G' from input 'u1' to output ...
16
y1: --------------
s^2 + 3 s + 16
Continuous-time model.
>> bode(G)
which gives
A dot was missing. It is mandatory when you want an element by element operation like (./).
See the difference between x / y and x ./ y in https://www.gnu.org/software/octave/doc/interpreter/Arithmetic-Ops.html.
function G = G(s)
G = 16./(s.^2+3*s+16);
endfunction
s_ = 0:0.01:4;
plot(s_, G(s_))

feval with multivariate function and vectorization

I need feval called with a bivariate function f and two vectors v1 and v2 (let us say that v1 are the x's and v2 the y's) to return a vector z=[f(v1(1),v2(1)) ... f(v1(n),v2(n)].
How can I do that ?
The following example does not work:
function z = f(x,y)
if x>0.5
z=x+y;
else
z=x+2*y;
end
end
indeed,
feval(f,[0.4 2 3],[4 5 6])
returns: [8.4 12 15]
instead of [8.4 7 9].
What is the correct syntax ?
The if condition doesn't work element-wise, as you seem to expect. Rather, the if branch is entered only if all the elements in the expression (x>0.5 in your case) evaluate to true.
To achieve what you want, change your function to
function z = f(x,y)
ind = x>.5;
z(ind) = x(ind) + y(ind);
z(~ind) = x(~ind) + 2*y(~ind);
end
Note that the logical index ind takes the place of your if.
For your particular function, the code could be simplified to
function z = f(x,y)
z = x + y + (x<=.5).*y;
end

create a polynomial matlab

I have got vector of coefficients v=[v1, v2, v3] (added by user).
I want to create a polynomial in a function. I would like to have a function fun(x), which solution will be my polynomial. After that I want to have a graph of this polynomial.
This is my idea but it doesn't work. Could you have any ideas how to improve it?
function [v] = createPolynomial(x)
r = length(v);
fun=0;
for i=r:1
fun=fun+v(i)*x.^(r-1);
end
You're pretty close! Is this what you want?
function f = createPoly(v,x)
n = length(v);
f = 0;
for ii = 1:n
f = f + v(ii)*x.^(n-ii+1);
end
end
f = createPoly([1 2 3 5],4)
f =
113
%% (1*4^3) + (2*4^2) + (3*4^1) + (5*4^0) = 113
Some errors in your code:
function [v] = createPolynomial(x)
As I understand it, you want both v and x as inputs to your function, and get a value back. Then you must do function value = createPolynomial(v, x), where valuewill be the output variable.
fun=fun+v(i)*x.^(r-1);
I guess this is just a typo, but .^r-1 is a constant value. You probably want the exponent to go from n, n-1, ... 1, 0 In that case you want r-i. And if I don't point it out, someone else will definitely do: Using i as a variable in MATLAB is not good practice if you are sometimes dealing with complex numbers.
And I guess you know this, but I'll say it anyway: You m-file must have the same name as you function.
If you want to input x as a vector, you must initialize f as a vector having the same length as x. That is:
f = zeros(1,length(x));
Now, you can do:
f = createPoly([1 2 3 5],1:5)
f =
11
27
59
113
195
you define coefficients in the following form of variable p
% example :
p =[ 2 1 3] % coefficients
x=0:0.2:5; % values at which it is to be evaluated
y=polyval(p, x);
plot(x,y)
polyval is provided in standard matlab, it evaluates the polynomial. see help polyval