Solving the equations in MATLAB 2018a`
Show older comments
I want to solve the following equations
eqn1 = a0-1
eqn2 = a1-1
psc = [ 6*a0 - 5*a1 + 2*a2, 6*a1 - 10*a2 + 6*a3, 6*a2 - 15*a3, 6*a3]% which will vary.
To solve the above 4( eqn1, eqn2 fixed and psc ) equations, I am using
s1 = struct2cell(solve(ics1,ics2,psc(1:(n-1))));
which is not serving the purpose.
Thank you in advance
Answers (1)
Star Strider
on 29 Dec 2019
‘To solve the above 4( eqn1, eqn2 fixed and psc ) equations ...’
The ‘fixed’ equation does not appear to have survived being posted.
For the others, try this:
psc = @(a0,a1,a2,a3) [a0-1; a1-1; (6*a0 - 5*a1 + 2*a2); (6*a1 - 10*a2 + 6*a3); (6*a2 - 15*a3); 6*a3];
av0 = rand(4,1)*100;
[av, fval] = fsolve(@(av) psc(av(1),av(2),av(3),av(4)), av0)
The parentheses are so MATLAB does not have problems with the vertical concatenation, since the spaces would be seen as delimiters, and the differing numbers of columns would throw an error.
Add ‘fixed’ to the ‘psc’ matrix, choose the correct initial estimates for ‘av0’, and fsolve should return the correct results.
8 Comments
Rag K
on 30 Dec 2019
Walter Roberson
on 30 Dec 2019
eq1=C*G;
That can only work if size(C,2) == size(G,1) [or G is scalar]. Since G would have to be known before you can do the multiplication, you can take its size() to find out what the m value is, instead of prompting for it or passing it in:
m = size(G,1) - size(x0,2);
Rag K
on 30 Dec 2019
Star Strider
on 30 Dec 2019
@Raghavendar K — I am not certain what you are asking in your first Comment, and what you want to do.
@Walter — Thank you!
(It is the end of my day. I will return to this in a few hours.)
Walter Roberson
on 30 Dec 2019
The only possible way you can do C*G is if G is defined at that point.
You intend it to be a matrix, but if you were to try to define it as single symbolic variable intending to replace it later with an array, then that will not work: all symbols are always treated as scalars.
Therefore the only way you can proceed is if at the point of the C*G, you have already defined G as an array the size you want.
Now you define C has having (size(x0,2) + m) columns and one row -- otherwise the [c0 X] cannot work.
You now have a 1 by (size(x0,2)+m) array being matrix multiplied with a something by something-else array, G. But that can only work if G has the same number of rows that C has columns. So G must have (size(x0,2)+m) rows.
But we know that G exists before you ask the user for the value m and we also know that x0 exists before you prompt for m . You therefore do not need to prompt for m at all, because you can calculate it as size(G,1) minus size(x0,2) .
The exception to this would be if you had some code between prompting for m and the C*G line that defines G knowing the value of m .
Rag K
on 2 Jan 2020
Rag K
on 2 Jan 2020
Star Strider
on 2 Jan 2020
@Rag K — My pleasure.
@Walter — I very much appreciate your contributions.
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!