fsolve question ('undefined function or variable')

Hi, i'm trying to solve this code:
function g=llamada
x0=[0.1 0.03]
[dfinal]=fsolve(@(d) febrerodosmilcatorce2(q,d),x0)
function f=febrerodosmilcatorce2(q,d)
L1=10;
D=0.02;
L2=1.5;
L3=0.3;
C1=10;
C2=4000;
G=9.81;
L=0.03;
A=8/(pi^2*G*D^4);
AD=8/(pi^2*G*d^4);
f=[-L*L1*A*q^2/D+C1-C2*q^2-L*L2*A*q^2/(9*D)-L*L3*A*q^2/(9*D)-A*q^2;-L*L1*A*q^2/D+C1-C2*q^2-4*L*L3*AD*q^2/(9*d)-L*L2*AD*q^2/(9*d)-4*L*L3*AD*q^2/(9*d)-A*q^2];
I want that the first function calls the second one giving it two inputs (q and d) and that one returns me the final value of d.¿Can anyone help me?
Thanks

Respuestas (2)

Alan Weiss
Alan Weiss el 24 de En. de 2017
It could be that the reason you want to run this complicated syntax is that you have not understood that MATLAB optimization functions optimize a single variable, usually called x. So if you want to solve for two variables, q and d, you can put them into a single vector x along the following lines:
function F = theObjective(x)
q = x(1);
d = x(2);
L1=10;
D=0.02;
L2=1.5;
L3=0.3;
C1=10;
C2=4000;
G=9.81;
L=0.03;
A=8/(pi^2*G*D^4);
AD=8/(pi^2*G*d^4);
F = [-L*L1*A*q^2/D+C1-C2*q^2-L*L2*A*q^2/(9*D)-L*L3*A*q^2/(9*D)-A*q^2;...
-L*L1*A*q^2/D+C1-C2*q^2-4*L*L3*AD*q^2/(9*d)-L*L2*AD*q^2/(9*d)-4*L*L3*AD*q^2/(9*d)-A*q^2];
Call the solver like this:
x0 = [0.1 0.03]
x = fsolve(@theObjective,x0)
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation

1 comentario

alvaro laguna
alvaro laguna el 24 de En. de 2017
Thank you Alan, i didn't Know that 'limitation' in the Matlab syntax.
Regards.

Iniciar sesión para comentar.

Geoff Hayes
Geoff Hayes el 24 de En. de 2017
alvaro - what is the full error message? fsolve is part of the Optimization Toolbox, so you will need it in order to have access to this function. If you don't have this toolbox, then you are most likely seeing the
Undefined function 'fsolve' for input arguments of type 'function_handle'.
error.
From the command line, type ver to see which toolboxes you have installed. If you have this toolbox then type
which fsolve -all
to see if you may have a conflicting function.

1 comentario

alvaro laguna
alvaro laguna el 24 de En. de 2017
Hi Geoff first of all thanks for your response. I have already the 7.1 version of the Optimitation toolbox. I think that it could be the way i call the second function because if I write both like this:
function g=llamada
x0=[0.1 0.03]
[dfinal]=fsolve(@(q) febrerodosmilcatorce2(q),x0)
function f=febrerodosmilcatorce2(q)
L1=10; D=0.02;
L2=1.5;
L3=0.3;
C1=10;
C2=4000;
G=9.81;
L=0.03;
A=8/(pi^2*G*D^4);
AD=8/(pi^2*G*q(2)^4);
f=[-L*L1*A*q(1)^2/D+C1-C2*q(1)^2-L*L2*A*q(1)^2/(9*D)-L*L3*A*q(1)^2/(9*D)-A*q(1)^2;-L*L1*A*q(1)^2/D+C1-C2*q(1)^2-4*L*L3*AD*q(1)^2/(9*q(2))-L*L2*AD*q(1)^2/(9*q(2))-4*L*L3*AD*q(1)^2/(9*q(2))-A*q(1)^2];
I get what i think that is the correct answer (q(1)=0.0011 and q(2)=0.0233) the problem is that i want only that the second function returns me the second value (q(2)) which i namered before as d.
Regards

Iniciar sesión para comentar.

Etiquetas

Preguntada:

el 24 de En. de 2017

Comentada:

el 24 de En. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by