How to simplify functions in matlab? - matlab

Hello Let's say I have theres two functions
F1= a*x^(2) + b
F2 = c*x
Where a, b and c are a constant and x is a variablem how do can I make matlab gives me a simplified version of F1*F2 so the answer may be
a*c*x^(3) + b*c*x
This is what I have in matlab
syms x a b c
F1 = a*x^(2) +b;
F2 = c*x^(2);
simplify(F1*F2)
ans =
c*x^2*(a*x^2 + b)
When I multiply in matlab it's just giving me (ax^(2) + b)(c*x)

Try this commands:
syms a x b c
F1= a*x^(2) + b
F2 = c*x
F=F1*F2
collect(F)
which will give you:
ans =
a*c*x^3 + b*c*x
The command collect is useful when working with polynoms. The opposite command is pretty. It will give you c*x*(a*x^2 + b)

Related

Finding Cubic Polynomials

I have an equation: y=ax^3 + bx^2 + cx + d and the list of values x = 1, 2, 3, 4 when y = 3, 4, 3, -6 respectively. In Octave, I want to:
(a) Set up a system of four equations involving a, b, c and d. For example, substituting (x, y) = (1,3) into the polynomial gives the equation 3 = a + b + c + d.
(b) Solve the system in (a).
I've been trying to find how to do this for three hours and found nothing. Any help would be appreciated
Thanks.
pstscrpt - I have to do everything in Octave, even though I could find it by hand
Written without any ; at end of assignements so you can see what is going on.
You problem is basically a linear system in the variables [a,b,c,d]'=z
So you need to build a system A*z=y, where A is a matrix 4x4, y and z are column vector size 4
x=[1,2,3,4]'
y=[3,4,3,-6]'
A=zeros(4,4)
for i=1:4
A(i,:)= [ x(i)^3, x(i)^2, x(i), 1]
endfor
z=A\y
the outcome will be
z =
-1.00000
5.00000
-7.00000
6.00000
In Matlab: start by just substituting the different values of x and y you wrote in the expression a*x^3 + b*x^2 + c*x + d = y as:
syms a b c d
eqn1 = a*1^3 + b*1^2 + c*1^1 +d == 3 ;
eqn2 = a*2^3 + b*2^2 + c*2^1 +d == 4 ;
eqn3 = a*3^3 + b*3^2 + c*3^1 +d == 3 ;
eqn4 = a*4^3 + b*4^2 + c*4^1 +d == -6 ;
Then Use equationsToMatrix to convert the equations into the form AX = B. The second input to equationsToMatrix specifies the independent variables in the equations.:
[A,B] = equationsToMatrix([eqn1, eqn2, eqn3, eqn4], [a, b, c,d ])
and the solution for a,b,c,d is:
X = linsolve(A,B)
you can also use if you want
sol = solve([eqn1, eqn2, eqn3, eqn4], [a, b, c,d ])

Human sensible outputs from finverse

I am using finverse and it is giving strange simplifications as an example
>> syms x
>> f = -0.0185*x^2 + 12.4698;
>> finverse(f)
ans =
(2^(1/2)*185^(1/2)*(62349 - 5000*x)^(1/2))/185
Rather than the much simpler
sqrt(12.4698 - x) / sqrt(0.0185)
Or something to that effect. Is there a way to force "human sensible outputs"? I tried simplify but it didn't help very much.
You can used function simple
syms x
f = -0.0185*x^2 + 12.4698;
Z = finverse(f)
V = simple(Z);
Z =
(2^(1/2)*185^(1/2)*(62349 - 5000*x)^(1/2))/185
>> V
V =
(23069130 - 1850000*x)^(1/2)/185
Remove ; at the end of function calls, that to see at different simplification.

What's the correct syntax to declare multiple line anonymous functions?

EDIT:
In regards to the linked question referenced above, I'm trying to describe a different computation than the one above. The one above is of form:
func1 >> func2 >> func3
where >> denotes continuation. Most importantly, func2 do not bind to any variable output by func1, and so on.
My function is of form:
func1 >>=# x func2(x) >>=# y func3(y)
where >>=# denotes continuation from func1 and func2, and func2 binds to the output of func1. So the key here is variable assignment needs to persist between function calls.
Question Proper.
I have this function declaration:
function[y] = distR3(x,c)
a = bsxfun(#minus, x, reshape(c, [1 1 3]));
b = a .* a;
c = b(:,:,1) + b(:,:,2) + b(:,:,3);
y = sqrt(c);
end
That I would like to convert to some anonymous function, perhaps like:
% Incorrect syntax
distRN = #(x,c)
a = bsxfun(#minus, x, reshape(c, [1 1 3]));
b = a .* a;
c = b(:,:,1) + b(:,:,2) + b(:,:,3);
y = sqrt(c);
return y
The key thing to note here is that there's multiple lines in the function, with variable assignment and possibly in-place update of variables.
Is such a thing possible?
EDIT EDIT:
I seem to have found an answer that uses "higher-order-functions". Here's the final code:
distRN = #(x,y) sqrt(c((b(a(x,y)))));
a = #(x,c) bsxfun(#minus, x, reshape(c, [1 1 3]));
b = #(a) a .* a;
c = #(b) b(:,:,1) + b(:,:,2) + b(:,:,3);
Note the indentations over a,b,and c are mine and have no effect.

Matlab: Fsolve giving incorrect roots

I'm trying to solve this system:
x = a + e(c - e*x/((x^2+y^2)^(3/2)))
y = b + c(d - e*y/((x^2+y^2)^(3/2)))
I'm using fsolve, but not matter what I put in as the starting points for the iteration, I get the answer that the starting points are the roots of the equation.
close all, clear all, clc
a = 1;
b = 2;
c = 3;
d = 4;
e = 5;
fsolve(#u1FsolveFUNC, [1,2])
Function:
function outvar = u1FsolveFUNC(invar)
global a b c d e
outvar = [...
-invar(1) + a + e*(c - e*(invar(1) / ((invar(1)^2 + invar(2)^2)^(3/2)))) ;
-invar(2) + b + e*(d - e*(invar(2) / ((invar(1)^2 + invar(2)^2)^(3/2))))]
end
I could try with [1,2] as invariables, and it will say that that is a root to the equation, alltough the correct answer for [1,2] is [12.76,15.52]
Ideas?
If you write your script like this
a = 1;
b = 2;
c = 3;
d = 4;
e = 5;
f = #(XY) [...
-XY(1) + a + e*(c - e*(XY(1) ./ (XY(1).^2 + XY(2).^2).^(3/2)))
-XY(2) + b + e*(d - e*(XY(2) ./ (XY(1).^2 + XY(2).^2).^(3/2)))];
fsolve(f, [1,2])
it is a lot clearer and cleaner. Moreover, it works :)
It works because you haven't declared a,b,c,d and e to be global before you assigned values to them. You then try to import them in your function, but at that time, they are still not defined as being global, so MATLAB thinks you just initialized a bunch of globals, setting their initial values to empty ([]).
And the solution to an empty equation is the initial value (I immediately admit, this is a bit counter-intuitive).
So this equation involves some inverse-square law...Gravity? Electrodynamics?
Note that, depending on the values of a-e, there may be multiple solutions; see this figure:
Solutions are those points [X,Y] where Z is simultaneously zero for both equations. for the values you give, there is a point like that at
[X,Y] = [15.958213798693690 13.978039302961506]
but also at
[X,Y] = [0.553696225634946 0.789264790080377]
(there's possibly even more...)
When you are using global command you have to use the command with all the variables in each function (and main workspace).
eg.
Main script
global a b c d e % Note
a = 1; b = 2; c = 3; d = 4; e = 5;
fsolve(#u1FsolveFUNC,[1,2])
Function
function outvar = u1FsolveFUNC(invar)
global a b c d e % Note
outvar = [-invar(1) + a + e*(c - e*(invar(1) / ((invar(1)^2 + invar(2)^2)^(3/2)))) ; -invar(2) + b + e*(d - e*(invar(2) / ((invar(1)^2 + invar(2)^2)^(3/2))))]

Matlab: Polynomial Expansion Routine

In Mathematica, it's easy to expand terms like
(ax^2+bx+c)^n
But is there anyway I can do this in Matlab?
For any arbitrary expression: not without the Symbolic Toolbox.
http://www.mathworks.com/help/toolbox/symbolic/expand.html
However, if you wish to expand polynomials, you can use the conv function. Just run it in a loop.
a = 1;
b = 2;
c = 3;
n = 5;
soln = [a b c];
for i=1:n-1
soln = conv(soln,[a b c]);
end
You can also use my sympoly toolbox.
>> sympoly a b c x
>> (a*x^2+b*x+c)^3
ans =
c^3 + 3*b*c^2*x + 3*b^2*c*x^2 + b^3*x^3 + 3*a*c^2*x^2 + 6*a*b*c*x^3 + 3*a*b^2*x^4 + 3*a^2*c*x^4 + 3*a^2*b*x^5 + a^3*x^6