Solving the equations in MATLAB 2018a`

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)

‘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

Thank you for your response, partially my problem is solved.
Now I am in this situation I could not able to make it generalize with this procedure. I wrote the following sample code for the value m=3. How can I generalize it for random input number m?
m=input('Enter the value m')
X = sym('c',[1 m]);
C=[c0 X];
%G =[-x^3+3*x^2-3*x+1; 3*x^3-6*x^2+3*x;-3*x^3+3*x^2;x^3]
eq1=C*G; %y(x)
K=expand(C*A*G+(1/x)*C*B*G+C*G)-5/4-x^2/16;% A and B are matrices of size m
for i=1:m-1
cc(i)=(1/2)+((1/2)*cos((i*pi)/m))
end
psc=subs(K,x,cc)
ics1=subs(diff(eq1),x,0)-0;
ics2=subs(eq1,x,1)-17/16;
sol1=struct2cell(solve(ics1,ics2,psc(1),psc(2)));
sol = vpa(subs(eq1,C,sol1'),3)% this is my solution y(x)
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);
Thank you Walter Roberson, for your reply. G will change with m, it is not fixed.
%G =[-x^3+3*x^2-3*x+1; 3*x^3-6*x^2+3*x;-3*x^3+3*x^2;x^3] this is for m=3.
Note:
C, G, A, B, and cc will change with the value of m.
I could able to manipulate it manually for m=4,5..etc.but could not able to generalize.
I may need to increase the value of m some times to m=30.
@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.)
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 .
@Walter Roberson , Thank you for explaining the flow which I need to take care when I am writing the code. I am working on my project as of now its fine with this
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) .
If I need this I will let you know in some time
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.
Thank you so much
I was little confused when I posted what really I need in that situatioin.
Thank you
@Rag K — My pleasure.
@Walter — I very much appreciate your contributions.

Sign in to comment.

Products

Release

R2018a

Asked:

on 29 Dec 2019

Commented:

on 2 Jan 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!