# Problem in using solve to find a solution to four equations !!!

2 visualizzazioni (ultimi 30 giorni)
Aravind Sasikumar il 3 Mar 2016
Hello,
Below is the code to solve for x1,x2,x3,x4 with four equations. The other terms appearing in the equations are constants and are calculated in the same code. I tried this with fsolve, and I am able to get solutions, but when trying with solve, I am getting this error ¨ Attempt to execute SCRIPT solve as a function:¨.The code is given below,
N=16;
v1a=0.15928512;
v3a=-0.11500684;
v1d=0.22888673;
v3d=-0.26834060;
t=0.1;
h=t*N;
zi=[(-t*(N/2)):t:(t*(N/2))];
za=2*t;
zd=zeros(1,N/4);
for i=1:2:((N/2)-1)
zd(i)=4*((zi(i+2)^3)-(zi(i)^3));
end
zd(N/4)=((zi((N/2)+3)^3)-(zi((N/2)-1)^3));
syms y1 y2 y3 y4
eq1=((v1a*h)-(za*((2*cos(2*y1))+(2*cos(2*y2))+(2*cos(2*y3))+(2*cos(2*y4))))==0); %v1a
eq2=((v3a*h)-(za*((2*cos(4*y1))+(2*cos(4*y2))+(2*cos(4*y3))+(2*cos(4*y4))))==0); %v3a
eq3=(((v1d*h^3)/4)-(zd(1)*(cos(2*y1)))-(zd(2)*(cos(2*y2)))-(zd(3)*(cos(2*y3)))-(zd(4)*(cos(2*y4)))==0); %v1d
eq4=(((v3d*h^3)/4)-(zd(1)*(cos(4*y1)))-(zd(2)*(cos(4*y2)))-(zd(3)*(cos(4*y3)))-(zd(4)*cos(4*y4)))==0); %v3d
result = solve([eq1, eq2, eq3, eq4], [y1, y2, y3, y4]);
y1res = result.y1
y2res = result.y2
y3res = result.y3
y4res = result.y4
The error :
Attempt to execute SCRIPT solve as a function:
Error in solve (line 96)
result = solve([eq1, eq2, eq3, eq4], [y1, y2, y3, y4]);
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposte (3)

John D'Errico il 3 Mar 2016
It is generally a BAD idea to name your script solve. Then when you try to use the function solve, do you think that MATLAB will be confused?
Try this:
which solve -all
Don't use names that cause conflicts.
##### 2 CommentiMostra NessunoNascondi Nessuno
Aravind Sasikumar il 3 Mar 2016
Hey,
Thanks, i completely forgot that I named it solve. I changed the name, the code gets executed, but still it shows two warnings:
Warning: 8 equations in 4 variables.
Warning: Explicit solution could not be found.
John D'Errico il 3 Mar 2016
Modificato: John D'Errico il 3 Mar 2016
But that is not a "problem" but a fact. You have an over-determined system, worse, a nonlinear one. Apparently there are no solutions. This is to be entirely expected. Think of it like this - there simply is not sufficient flexibility among those 4 variables to enable you to solve all 8 equations exactly.

Accedi per commentare.

Walter Roberson il 3 Mar 2016
Your initialization of zd is incorrect. You create it as a vector of zeros length N/4 (i.e., length 4) but you populate every second element of it up to N/2 - 1 (i.e., length 7), and then you store at N/4 (i.e., 4). So element 1, 3, 4, 5, and 7 are populated and element 2 and 6 are left 0.
I speculate that you wanted
for i = 1 : ((N/4)-1)
zd(i) = 4*((zi(2*i+2)^3)-(zi(2*i)^3));
end
##### 4 CommentiMostra 2 commenti meno recentiNascondi 2 commenti meno recenti
Walter Roberson il 4 Mar 2016
The code I speculated should accomplish the same thing but more compactly.
By the way, my tests show that your system is fairly stable if you have errors in your v1a and so on (you are not going to try to tell us that v1a = 15928512/100000000 exactly are you?)
There are 4 real-valued solutions to your equations for your variables y1, y3, and y4, but only 2 real-valued solutions for your variable y2.
(Oddly, if you make your v3d more negative, you can get into a zone where there are three real-valued solutions even though you are dealing with a quartic. The roots of the quartic all come out real-valued in that range, but one of the four roots triggers arcsin() of a value > 1 so that one root of the quartic leads to a complex-valued solution to the overall problem. This is, I know, a meaningless point, but it threw me for a while as I was trying to solve the problem under the assumption that your v1a, v3a, v1d, v3d were approximations instead of exact values.)
Aravind Sasikumar il 8 Mar 2016
Hey, No they are not very exact values, but I liked the way you worked on it to get a solution. The V values are given to me by the professor and I cant really do a lot to them because they are already optimized values from another optimization code. Anyway thanks a lot, and I solve fsolve is the solution.

Accedi per commentare.

Aravind Sasikumar il 3 Mar 2016
Hello,
Yeah, its a highly non-linear one, I just wanted to see how solve and fsolve gives me the solutions. With fsolve, I had to repeat the whole set of equations in a kind of loop with different initial points to get the best solution, which was time consuming. But it looks like thats the only way. Anyway thanks a lot for your valuable comments.
##### 3 CommentiMostra 1 commento meno recenteNascondi 1 commento meno recente
Aravind Sasikumar il 4 Mar 2016
Hello,
I dont really get it, like pick any 4 equations. There are only equations namely, eq1, eq2, eq3, eq4, with 4 unknown variables y1,y2,y3,y4.
Walter Roberson il 4 Mar 2016
You had extra equations being generated because your zd was larger than you thought.

Accedi per commentare.

### Categorie

Scopri di più su Equation Solving in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by