With Matlab, I have a matrix solution to find from 2 matricial equations (size matrix is 7x7). Here the 2 equations to solve with "a" and "b" are the unknow matrices and where F1, F2, P1 and P2, D, D2, D are known. Solving "a" and "b" would allow me to build a new matrix P = a . P1 + b . P2 . (remark : D matrix is equal to : D = a.a.D1 + b.b.D2 with D1and D2 diagonal matrices) :
a.a + a.P1.b.P2^T + b.P2.a.P1^T + b.b - Id = 0 (equation 1)
F1.a.P1 + F1.b.P2 + F2.a.P1 + F2.b.P2 − (a.P1 + b.P2).D = 0 (equation 2)
Question 1) Is there a solver in Matlab that would allow to directly find matrices "a" and "b" ?
Question 2) If there is no directly solving, I think that I have to solve each solution a(i,j) and b(i,j) by using a double loop on i and j indices and call fsolve in inner loop.
The problem is that I have to respect the order in matricial product and it is difficult to not make errors when I implement the code.
Here the 2 equations that I must solve on "a" and "b" unknown :
equation(1)
equation(2)
So I tried to implement the solving of each matrix element, i.e a(i,j) and b(i,j)
For this, I am using a symbolic array x[7 7 2] and did :
% eigenv_sp = P1
% eigenv_xc = P2
% FISH_sp = F1
% FISH_xc = F2
% FISH_eigen_sp = D1
% FISH_eigen_xc = D2
% Fisher matrices
F1 = FISH_sp;
F2 = FISH_xc;
% Transposed matrix
transp_eigenv_sp = eigenv_sp'
transp_eigenv_xc = eigenv_xc'
% Symbolic variables
x = sym([7 7 2]);
aP1 = sym([7 7]);
aP1T = sym([7 7]);
bP2 = sym([7 7]);
bP2T = sym([7 7]);
d1 = sym([7 7]);
d2 = sym([7 7]);
d = sym([7 7]);
ad1 = sym([7 7]);
bd2 = sym([7 7]);
for k=1:7
for l=1:7
aP1(k,l) = sum(x(k,1:7,1).*eigenv_sp(1:7,l));
bP2(k,l) = sum(x(k,1:7,2).*eigenv_xc(1:7,l));
aP1T(k,l) = sum(x(k,1:7,1).*transp_egeinv_sp(1:7,l));
bP2T(k,l) = sum(x(k,1:7,2).*transp_egeinv_xc(1:7,l));
a2(k,l) = sum(x(k,1:7,1).*x(1:7,l,1));
b2(k,l) = sum(x(k,1:7,2).*x(1:7,l,2));
d1(k,l) = FkSH_ekgen_sp(k,l);
d2(k,l) = FkSH_ekgen_xc(k,l);
d(k,l) = d1(k,l) + d2(k,l);
ad1(k,l) = sum(x(k,1:7,1).d1(1:7,l));
bd2(k,l) = sum(x(k,1:7,2).d2(1:7,l));
end
end
% Function that represents `(a,b)(i,j)` unknown element represented by `(y(i,j,1),y(i,j,2))`
myfun=#(x,i,j) [
% First equation
sum(x(i,1:7,1).*x(1:7,j)) + sum(aP1(i,1:7).*bP2T(1:7,j)) + sum(bP2(i,1:7).*aP1T(1:7,j)) + sum(x(i,1:7).*x(1:7,2)) - eq(i,j);
% second equation
sum(F1(i,1:7).*aP1(1:7,j)) + sum(F1(i,1:7).*bP2(1:7,j)) + sum(F2(i,1:7).*aP1(1:7,j)) + sum(F2(i,1:7).*bP2(1:7,j)) - ...
sum(aP1(i,1:7).*ad1(1:7,j)) + sum(bP2(i,1:7).*ad1(1:7,j)) + sum(adP1(i,1:7).*dP2(1:7,j)) + sum(bP2(i,1:7).*dP2(1:7,j));
]
% Solution of system of non linear equations with loop
y = zeros(7, 7, 2);
for i=1:7
for j=1:7
a0 = 1e7;
b0 = 1e7;
x0 = [ a0 b0];
y(i,j,:) = fsolve(#(x)myfun(x,i,j),x0)
end
end
But at the execution, I have the following error :
Index exceeds matrix dimensions.
Error in sym/subsref (line 814)
R_tilde = builtin('subsref',L_tilde,Idx);
Error in compute_solving_Matricial_Equations_dev (line 60)
aP1(k,l) = sum(x(k,1:7,1).*eigenv_sp(1:7,l));
I don't understand where is the issue, I only iterate on index 1:7, if someone could see what's wrong ?
Update 1
I wonder if I have the right to use for a given index i the symbolic expression x(1:7,i,1) whereas this array is not known numerically but defined only from a symbolic point of view.
Update 2
Maybe I have a clue.
If I do an expand of the symbolic array x (of size (7,7,2)), I get :
>> expand(sum(x(1,1:7).*x(1:7,1)))
Index exceeds matrix dimensions.
Error in sym/subsref (line 814)
R_tilde = builtin('subsref',L_tilde,Idx);
So, the only thing to done I think is to explicitly write the 7x7 equations
with 7x7 = 49 couples unknown x(i,j,1) and x(i,j,2) which corresponds to a(i,j) and b(i,j) matrices.
Update 3
I think the key point is to put into function myfun the 2 matrical equations but with taking into account of the 49 (7x7) couples (a(i,j) and b(i,j)) unknown that I defined previously as symbolic variables with array x[7 7 2].
And second point, I must to "make understand" to fsolve that we are searching for each iteration (double loop on (i,j)) the value of a(i,j) and b(i,j) given the whole 49 equations. Maybe it would be necessary to expand for example the expression of x(i,1:7) or x(1:7,j) in myfun function but I don't know how to proceed.
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)
I am trying to use minimization to calculate coefficients of the polynomial p(x) = 1 + c(1)x + c(2)x^2 to approximate e^x. I need to use points xi = 1 + i/n for natural numbers i on [1,n], first for n=5, then n=10, etc. The approach is to minimize the 1, 2, and inf norm(p(x) - e^x) using fminsearch. So the output should be the 2 coefficients for the 3 p(x)'s. Any suggestions is appreciated.
Well, if anyone was wondering, I did figure the question out finally.
for l = [1 2 inf]
fprintf('For norm %d\n', l)
fprintf('Coefficients c1 c2\n')
for n = [5 10 100]
i = 1:n ;
x = 1 + i/n ;
c = [1 1] ;
%Difference function that we want to minimize
g = #(c) x.*c(1) + x.^2.*c(2) + 1 - exp(x);
f_norm = #(c) norm(g(c), l) ;
C = fminsearch(f_norm, c);
fprintf('n = %d ', n)
fprintf('%f %f\n', C(1), C(2))
% Compare plot of e^x and p(x).
p = #(x) C(1)*x + C(2)*x.^2 + 1;
xx = linspace(1,2,1e5);
figure;
plot(xx, p(xx), '--r', xx, exp(xx));
str = sprintf('Plot with n = %d, for norm %d', n,l);
title(str,'FontSize',24)
xlabel('x','FontSize',20)
ylabel('y','FontSize',20)
legend('p2 approximation','exponential');
end
end
This worked in the end to answer the question.
I have a general equation
t=tr+(ts-tr)/(1+(a*h)^n)^(1-1/n)
for (h=0, 1, 2, 3), I have t=2.000, 1.6300, 1.2311, 1.1084. therefor there are 4 equations with 4 unknowns tr, ts, a, n
I used "solve" function in matlab
s=solve('tr+(ts-tr)/(1+(a*0)^n)^(1-1/n)=2','tr+(ts-tr)/(1+(a*1)^n)^(1-1/n)=1.63','tr+(ts-tr)/(1+(a*2)^n)^(1-1/n)=1.2311','tr+(ts-tr)/(1+(a*3)^n)^(1-1/n)=1.1084')
and error is
??? Error using ==> mupadmex
Error in MuPAD command: Singularity [ln];
during evaluation of 'numeric::fsolve'
Error in ==> sym.sym>sym.mupadmexnout at 2018
out = mupadmex(fcn,args{:});
Error in ==> solve at 76
[symvars,R] = mupadmexnout('symobj::solvefull',eqns,vars);
What should I do?
The problem appears with you using the solve function. That only works for simple equations, it is better to use the fsolve function. Due to the fact that I am worried that I am doing an assignment for you, I am only going to show you how to do another example using fsolve.
Suppose that you want to solve
1 = x_1
1 = x_1 + x_2
-1 = x_1 + x_2 + x_3
-1 = x_1 + x_2 + x_3 + x_4
then what you firstly need to do is write these as equations equal 0
0 = x_1 - 1
0 = x_1 + x_2 - 1
0 = x_1 + x_2 + x_3 + 1
0 = x_1 + x_2 + x_3 + x_4 + 1
then you need to write a function that takes in a vector x, the components of x will represent x_1, x_2, x_3 and x_4. The output of the function will also be a vector whose components should the outputs of the Right hand side of the above equations (see the function fun below). This function is going to be called by fSolve for it to provide it with guesses of the correct value of x, until it guess correct. When never actually run this function ourselves. That is why it is below the top function.
Then you create a function handle to this function by fHandle = #fun. You can think of fHandle as another name for fun, when we calculate fHandle([1; 2; 3; 4]) this is the same as calculating fun([1; 2; 3; 4]). After this you make an initial guess of the correct vector x, say we chose xGuess = [1; 1; 1; 1]. Finally we pass fHandle and xGuess to fSolve.
Here is the code
function Solve4Eq4Unknown()
fHandle = #fun;
xGuess = ones(4,1);
xSolution = fsolve(fHandle, xGuess)
end
function y = fun(x)
y = zeros(4,1); % This step is not necessary, but it is effecient
y(1) = x(1) - 1;
y(2) = x(1) + x(2) - 1;
y(3) = x(1) + x(2) + x(3) + 1;
y(4) = x(1) + x(2) + x(3) + x(4) + 1;
end
I have a question regarding Matlab's option to publish in PDF. Say I have the following code:
%1D functions and plotting
%1. We calculate y given the following function: y(x) = 2x^2 +
%3x + 1, for x = 10.
x = 10;
y = 2*x.^2 + 3*x + 1
%2. We calculate y given the following function: y(x) = ax^2 + bx + c, with a
%=2, b=3, c=0 and x = 100.
a=2;
b=3;
c=0;
x=100;
y = a*x.^2 + b*x + c
. . . more code follows here
When I choose to publish this as PDF the answers to problems 1 and 2 (where I calculate two different values for the variable 'y') do not appear at the line where I calculate the value (where I write y = 2*x.^2 + 3*x + 1 for instance). Instead, the values of the 'y' variables appear at the end of the document where it says 'y = 231' and 'y = 20300'. Is there any way I can get this to be included right after I define the variable without separating the document into cells? Or is this a default thing that I can not do anything about? I would really appreciate any input!
The general approach (indipendent of the output format) is to restart the paragraph with line break and %%:
%% 1. We calculate y given the following function: y(x) = 2x^2 +
% 3x + 1, for x = 10.
x = 10;
y = 2*x.^2 + 3*x + 1
%% 2. We calculate y given the following function: y(x) = ax^2 + bx + c, with a
% =2, b=3, c=0 and x = 100.
a=2;
b=3;
c=0;
x=100;
y = a*x.^2 + b*x + c