hello iam trying to solve a system of non linear equations and i don't know what the wrong so if any one can help me it will be appreciated..

1 view (last 30 days)
function f= myfunction(z)
c2=z(1);
c3=z(2);
c4=z(3);
c5=z(4);
c6=z(5);
f(1)= 9*cos(c2) + 14.2*cos(c3) - 14.5*cos(c4)==17.7 ;
f(2)= 9*sin(c2) + 14.2*sin(c3) - 14.5*sin(c4) ;
f(3)= 9*sin(c2) + 12*sin(c5) == 9.3 ;
f(4)= 9*cos(c2) - 12*cos(c5) + sqrt(9.3^2 / (1-(cos(c6))^2));
end
In the Command window I pass in c2 as an initial value :
c2=0:0.01:pi;
fsolve(@myfunction,c2)
It gives me this error
Error using fsolve (line 281)
FSOLVE requires all values returned by functions to be of data type double.

Answers (2)

Image Analyst
Image Analyst on 30 Dec 2019
What is the class of f?
Since you're using "==" when you define it, I'm guessing it's not a double. Why are you using == anyway when you define f?
And why are you passing in 315 values for c2 (which becomes z inside the function) when you are only extracting and using the first 5 elements of the total 315 elements in z?
  4 Comments
Ahmed ElNahas
Ahmed ElNahas on 30 Dec 2019
Sorry for misunderstanding
i have 4 equations with 4 unkonws (c3,c4,c5,c6) and c2 is my input so i want to solve this 4 non linear equations together to get the values of the unkowns thats all i want to do.
By knowing that c2 changes with time so that i write it in interval from 0 to pi .
Thats All and Thank you very much :)
Image Analyst
Image Analyst on 30 Dec 2019
If equation 1 is supposed to give 17.7, and equation 3 is supposed to give 9.3, what are equations 2 and 4 supposed to give?

Sign in to comment.


Matt J
Matt J on 30 Dec 2019
Edited: Matt J on 30 Dec 2019
It doesn't appear that there are exact solutions for c2 <= 1.26,
lb=zeros(1,4);
ub=2*pi*(lb+1);
c2=0:0.01:pi;
N=numel(c2);
resnorm=zeros(size(c2));
[z,ffinal]=deal(nan(4,N));
for i=N:-1:1;
ci=c2(i);
fun=@(z)myfunction(z,ci);
if i<N && norm(ffinal(:,i+1))<1e-4
z0=z(:,i+1);
else
[z0,resnorm(i)]=ga(@(z) norm(fun(z)),4,[],[],[],[],lb,ub); %global sweep
end
[z(:,i),ffinal(:,i)]=fsolve(fun,z0); %refine
end
plot(c2.',ffinal.','x'); legend('f1','f2','f3','f4')
ylabel 'Final f(i)'
xlabel 'c2'
function f= myfunction(z,c2)
c3=z(1);
c4=z(2);
c5=z(3);
c6=z(4);
f(1)= 9*cos(c2) + 14.2*cos(c3) - 14.5*cos(c4)-17.7 ;
f(2)= 9*sin(c2) + 14.2*sin(c3) - 14.5*sin(c4) ;
f(3)= 9*sin(c2) + 12*sin(c5) - 9.3 ;
f(4)= 9*cos(c2) - 12*cos(c5) + sqrt(9.3^2 / (1-(cos(c6))^2));
end
  2 Comments
Image Analyst
Image Analyst on 30 Dec 2019
Ahmed forgot to list his version number. My R2019b help does not list an fsolve() function. I tried to run it and it says it requires the Global Optimization Toolbox. I've added that to the Products list.
Matt J
Matt J on 30 Dec 2019
Edited: Matt J on 30 Dec 2019
@Image Analyst.
fsolve is in the Optimization Toolbox and ga is in the Global Optimization Toolbox,
@Ahmed,
If I change the final equation to
f(4)= (9*cos(c2) - 12*cos(c5)).^2 - (9.3^2 / (1-(cos(c6))^2));
then solutions are found over a wider range of c2.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!