I am trying to use the Matlab "gradient" and "hessian" functions to calculate the derivative of a symbolic vector function with respect to a vector. Below is an example using the sigmoid function 1/(1+e^(-a)) where a is a feature vector multiplied by weights. The versions below all return an error. I am new to Matlab and would very much appreciate any advice. The solution may well be under my nose in the documentation, but I have not been able to solve the issue. Thank you in advance for your help!
%version 1
syms y w x
x = sym('x', [1 3]);
w = sym('w', [1 3]);
f = (y-1)*w.*x + log(1/(1+exp(-w.*x)));
gradient(f, w)
%version 2
syms y w1 w2 w3 x1 x2 x3 x w
x = [x1,x2,x3];
w = [w1,w2,w3];
f = (y-1)*w.*x + log(1/(1+exp(-w.*x)));
%version 3
syms y w1 w2 w3 x1 x2 x3
f = (y-1)*[w1,w2,w3].*[x1,x2,x3] + log(1/(1+exp(-[w1,w2,w3].*[x1,x2,x3])));
Thanks, Daniel - it turns out the problem was in not having used dot() to take the dot product of w and x. Both diff() and gradient() gave the same solution, shown below:
syms y
x = sym('x', [1 3]);
w = sym('w', [1 3]);
f = (y-1)*dot(w,x) + log(1/(1+exp(dot(-w,x))));
diff(f, w(1))
gradient(f, w(1))
%ans =
%x1*(y - 1) + (x1*exp(- x1*conj(w1) - x2*conj(w2) - x3*conj(w3)))/
(exp(-x1*conj(w1) - x2*conj(w2) - x3*conj(w3)) + 1)
Related
I have a differential equation that looks like this:
dT/dx = (1+alpha\*M1)\*(T^2)\*S(x) - 4\*(2+gamma\*M2)\*x\*T
I want to differentiate this equation let's say over alpha. However, T depends on alpha. How can I get a differential equation that includes the derivative dT/da so I can solve for it (using ode45 or something)?
Note: I can't differentiate the equation by hand as I will need to do this for the Ys as well, where I don't have an easy analytical function.
My code so far:
syms L alpha gamma Y2 Y3 Y4 Y5 Y6 x T X
M1 = 0;
M2 = 7;
Y1 = 1 + M2 / 10;
Y7 = 3 + M1 / 10;
xcp = [0 0.1*L 0.25*L 0.5*L 0.6*L 0.75*L L];
ycp = [Y1 Y2 Y3 Y4 Y5 Y6 Y7];
T0 = 5 + 1/L - 25/(L^2);
S = bezier_syms(); % Assume this returns a function of L,x,Y2,Y3,Y4,Y5,Y6
ode = diff(T,x) == (1+alphaM1)*(T^2)*S(L,x,Y2,Y3,Y4,Y5,Y6) - 4*(2 + gamma*M2)xT;
Given the equation
dT/dx = F(x,T,a)
and U=dT/da, then by the rules of differentiation like the chain rule, you get
dU/dx = dF/da(x,T,a) + dF/dT(x,T,a)*U
where the partial derivatives are easy to determine. For more precise formulas, see https://math.stackexchange.com/a/3699281/115115
Could you please help me with the following question:
I want to solve a second order equation with two unknowns and use the results to plot an ellipse.
Here is my function:
fun = #(x) [x(1) x(2)]*V*[x(1) x(2)]'-c
V is 2x2 symmetric matrix, c is a positive constant and there are two unknowns, x1 and x2.
If I solve the equation using fsolve, I notice that the solution is very sensitive to the initial values
fsolve(fun, [1 1])
Is it possible to get the solution to this equation without providing an exact starting value, but rather a range? For example, I would like to see the possible combinations for x1, x2 \in (-4,4)
Using ezplot I obtain the desired graphical output, but not the solution of the equation.
fh= #(x1,x2) [x1 x2]*V*[x1 x2]'-c;
ezplot(fh)
axis equal
Is there a way to have both?
Thanks a lot!
you can take the XData and YData from ezplot:
c = rand;
V = rand(2);
V = V + V';
fh= #(x1,x2) [x1 x2]*V*[x1 x2]'-c;
h = ezplot(fh,[-4,4,-4,4]); % plot in range
axis equal
fun = #(x) [x(1) x(2)]*V*[x(1) x(2)]'-c;
X = fsolve(fun, [1 1]); % specific solution
hold on;
plot(x(1),x(2),'or');
% possible solutions in range
x1 = h.XData;
x2 = h.YData;
or you can use vector input to fsolve:
c = rand;
V = rand(2);
V = V + V';
x1 = linspace(-4,4,100)';
fun2 = #(x2) sum(([x1 x2]*V).*[x1 x2],2)-c;
x2 = fsolve(fun2, ones(size(x1)));
% remove invalid values
tol = 1e-2;
x2(abs(fun2(x2)) > tol) = nan;
plot(x1,x2,'.b')
However, the easiest and most straight forward approach is to rearrange the ellipse matrix form in a quadratic equation form:
k = rand;
V = rand(2);
V = V + V';
a = V(1,1);
b = V(1,2);
c = V(2,2);
% rearange terms in the form of quadratic equation:
% a*x1^2 + (2*b*x2)*x1 + (c*x2^2) = k;
% a*x1^2 + (2*b*x2)*x1 + (c*x2^2 - k) = 0;
x2 = linspace(-4,4,1000);
A = a;
B = (2*b*x2);
C = (c*x2.^2 - k);
% solve regular quadratic equation
dicriminant = B.^2 - 4*A.*C;
x1_1 = (-B - sqrt(dicriminant))./(2*A);
x1_2 = (-B + sqrt(dicriminant))./(2*A);
x1_1(dicriminant < 0) = nan;
x1_2(dicriminant < 0) = nan;
% plot
plot(x1_1,x2,'.b')
hold on
plot(x1_2,x2,'.g')
hold off
I am trying to solve a system of three equations with three unknown variables.
A1=(x+y)/2+(x-y)/2*cos(2*phi)+z*sin(2*phi)/2
A2=(x+y)/2-(x-y)/2*cos(2*phi)-z*sin(2*phi)/2
A3=-(x-y)/2*sin(2*phi)+z*cos(2*phi)
where A1, A2, A3, and phi are known and x,y, and z are unknown.
I used below code but it does not work. I got the solution as symbols.
clear;
clc;
A1=50;
A2=37.5;
A3=125.6;
phi=28;
syms x y z
eqn1 = (x+y)/2+(((x-y)/2)*cosd(2*phi))+(z*sind(2*phi))/2== A1;
eqn2 = (x+y)/2+(((x-y)/2)*cosd(2*phi))-(z*sind(2*phi))/2== A2;
eqn3 = (((x-y))*sind(2*phi))+(z*cosd(2*phi))== A3;
[A,B] = equationsToMatrix([eqn1, eqn2, eqn3], [x, y, z]);
X_1 = linsolve(A,B);
Thanks
You can skip using equationsToMatrix and linsolve and just use solve. You are already using the symbolics toolbox, so why would you want to convert the system into a matrix of coefficients and solve it that way? Just use the actual equations directly.
A1=50;
A2=37.5;
A3=125.6;
phi=28;
syms x y z
eqn1 = (x+y)/2+(((x-y)/2)*cosd(2*phi))+(z*sind(2*phi))/2== A1;
eqn2 = (x+y)/2+(((x-y)/2)*cosd(2*phi))-(z*sind(2*phi))/2== A2;
eqn3 = (((x-y))*sind(2*phi))+(z*cosd(2*phi))== A3;
[X, Y, Z] = solve(eqn1, eqn2, eqn3);
I get:
X = (sym)
69370560820559
──────────────
926177760500
Y = (sym)
-61526962823521
────────────────
926177760500
Z = (sym)
2910
────
193
Note that I'm using Octave instead of MATLAB (on my current system, I don't have access to the symbolic toolbox) so the output may be a bit different in format. You probably also want this in real (floating-point) form, so an additional cast to double for the outputs should help:
X = double(X);
Y = double(Y);
Z = double(Z);
By doing this, we get:
>> format long g;
>> X
X = 74.8998343288972
>> Y
Y = -66.4310518429048
>> Z
Z = 15.0777202072539
I try to use contour to plot this function
3y + y^3 - x^3 = 5
I try contour(3*y+y^3-x^3-5) but it doesn't work.
How can I use contour to plot this function?
Are x and y properly defined as 2x2 matrices? If so then the "power" operator needs to be done on a component-wise basis (.^3 instead of ^3).
This works:
[x,y] = meshgrid(-2:.2:2,-2:.2:2);
contour(3*y+y.^3-x.^3-5)
Maybe you can try fcontour, which plots the contour lines of the function z = f(x,y) for constant levels of z over the default interval [-5 5] for x and y.
f = #(x,y) 3*y + y.^3 - x.^3 - 5;
fcontour(f)
Output:
I'm not convinced this addresses all parts of your question but it's a start. If you absolutely want contour to call a function, you can adjust my example to contour(X,Y,fh(X,Y)).
Better Approach
fh=#(x,y) 3*y + y.^3 - x.^3 -5; % <--- This is your function
x = (-4:.25:4)';
y = (-2:.25:2)';
[X,Y] = meshgrid(x,y);
Z = fh(X,Y);
contour(X,Y,fh(X,Y))
The Direct Approach (not preferred but works)
Notice the Z is transposed to make this work.
fh=#(x,y) 3*y + y.^3 - x.^3 -5; % <----this is your function
X = (-4:.25:4)';
Y = (-2:.25:2)';
Z = zeros(length(X),length(Y));
for i = 1:length(X)
for j = 1:length(Y)
xi = X(i);
yj = Y(j);
Z(i,j) = fh(xi,yj);
end
end
contour(X,Y,Z','LevelList',-60:10:60,'ShowText','on','LineWidth',1.4) % Fancied it up a bit
I have a very simple problem.
I have
x=[ 10 25 50];
y=[ 1.2 3 7.5];
I know my curve fitting function
f(x)=(a*x+1)/(bx+c);
How can I get coefficient(a,b,c) solve in matlab and also plot this curve?
Rearrange y = f(x) to make a, b, and c the unknowns:
y = (ax + 1) / (bx + c)
y(bx + c) = ax + 1
ax - bxy - cy = -1;
This describes a system of simultaneous linear equations in a, b, and c when you substitute your three paired values of x and y.
x = [10, 20, 100];
y = [1.2, 0.7, 0.4];
coeffs = [x', (-x.*y)', -y'];
knowns = [-1, -1, -1]';
v = coeffs \ knowns; % v is [a; b; c]
Now you have the coefficients a, b, and c so you can plot the function.
Addendum: plotting
To plot a function, first choose the x-values of the data points
xt = 1:100;
Then calculate the y-values (assuming you've already got a, b, c)
yt = (a*x + 1) ./ (b*x + c)
Then just plot them!
plot(xt, yt);
Read the Matlab help on the plot function for customizing the style of the plot.