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

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]);

Answers (3)

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 Comments

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.
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.

Sign in to comment.

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 Comments

Your eq4 line is missing a ( right near the end, just before the final cos() term. The ( is there in the eq3 line.
Hello,
Thanks a lot for pointing out that,
I modified my code in this way,
zd=zeros(1,N/4);
j=1;
for i = 1 :2: ((N/2)-2)
zd(j)=2*((zi(i+2)^3)-(zi(i)^3));
j=j+1;
end
zd(N/4)=((zi((N/2)+3)^3)-(zi((N/2)-1)^3));
Thanks again
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.)
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.

Sign in to comment.

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 Comments

Pick any 4 of the equations. solve() those. You will probably get sets of solutions.
For each member of the set of solutions, substitute the variable values from the solution into the remaining equations and see if the remaining equations are satisfied. If they are not, then reject the candidate solution and otherwise accept it.
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.
You had extra equations being generated because your zd was larger than you thought.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!