Error using fsolve, indices not compatible

I am trying to solve a system of equations using fsolve but I keep getting the following error:
Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.
Error in root2d (line 4)
F(1)=(m*(x1-x3))-(y1-y3);
Below is a snippet of my code, with the function first followed by the main script.
function F = root2d(k,m,x1,y1)
x3 = k(1);
y3 = k(2);
F(1)=(m*(x1-x3))-(y1-y3);
F(2)=(sqrt(((y1-k(2))^2)+((x1-k(1))^2)))-1;
m = diff(y(xx1));
x0 = [0,0];
sol = fsolve(@(k)root2d(k,m,x1,y1),x0);
Note: x1 and x2 are scalars defined earlier in the code.
Can anyone help me fix this error? Thank you!

9 comentarios

madhan ravi
madhan ravi el 5 de Dic. de 2018
Dude provide the full code or attach your relevent script files
function F = root2d(k,m,x1,y1)
x3 = k(1);
y3 = k(2);
F(1)=(m*(x1-x3))-(y1-y3);
F(2)=(sqrt(((y1-k(2))^2)+((x1-k(1))^2)))-1;
r = 3;
f = 2;
x0 = 0;
x1 = -3;
x2 = 3;
y0 = 0;
y1 = x1^2;
y2 = x2^2;
rmag = sqrt(((x1-x0)^2)+((y1-y0)^2));
fi = asind(r/rmag);
thetas = 0.25;
x = x1:0.01:x2;
y = x.^2;
theta = randi([-90,90],1,1);
xx1 = (randi(length(x),1,1));
x1 = x(xx1);
y1 = y(xx1);
x2 = (sind(theta))+ x1;
f = @(y2) sqrt(((x1-x2)^2)+((y1-y2)^2))-1;
y2 = fsolve(f,1);
m = diff(y(xx1));
x0 = [0,0];
sol = fsolve(@(k)root2d(k,m,x1,y1),x0);
madhan ravi
madhan ravi el 5 de Dic. de 2018
Editada: madhan ravi el 5 de Dic. de 2018
but before the error I get this
Equation solved. The sum of squared function values, r = 5.652814e-22, is less than
sqrt(options.FunctionTolerance) = 1.000000e-03. The relative norm of the gradient of r,
2.261198e-11, is less than options.OptimalityTolerance = 1.000000e-06.
Optimization Metric Options
relative norm(grad r) = 2.26e-11 OptimalityTolerance = 1e-06 (default)
r = 5.65e-22 sqrt(FunctionTolerance) = 1.0e-03 (default)
>>
Abigale Mattingly
Abigale Mattingly el 5 de Dic. de 2018
Madhan, I don't understand your comment. I think you are missing part of your response? Thanks for the help!
Abigale Mattingly
Abigale Mattingly el 5 de Dic. de 2018
Do you know how I may correct the error or why I am getting it?
I dont have error. I'm bit confusing. You should save them in one script.
function F = root2d(k,m,x1,y1)
x3 = k(1);
y3 = k(2);
F(1)=(m*(x1-x3))-(y1-y3);
F(2)=(sqrt(((y1-k(2))^2)+((x1-k(1))^2)))-1;
end
Open another script and run it :
r = 3;
f = 2;
x0 = 0;
x1 = -3;
x2 = 3;
y0 = 0;
y1 = x1^2;
y2 = x2^2;
rmag = sqrt(((x1-x0)^2)+((y1-y0)^2));
fi = asind(r/rmag);
thetas = 0.25;
x = x1:0.01:x2;
y = x.^2;
theta = randi([-90,90],1,1);
xx1 = (randi(length(x),1,1));
x1 = x(xx1);
y1 = y(xx1);
x2 = (sind(theta))+ x1;
f = @(y2) sqrt(((x1-x2)^2)+((y1-y2)^2))-1;
y2 = fsolve(f,1);
m = diff(y(xx1));
x0 = [0,0];
sol = fsolve(@(k)root2d(k,m,x1,y1),x0);
Abigale Mattingly
Abigale Mattingly el 5 de Dic. de 2018
What do you mean I should save them in one script? You later said you opened another script.
Abigale Mattingly
Abigale Mattingly el 5 de Dic. de 2018
I'm still getting an error.
Kevin Chng
Kevin Chng el 5 de Dic. de 2018
Save them in the same path, and then run Untitled2.m.

Iniciar sesión para comentar.

 Respuesta aceptada

madhan ravi
madhan ravi el 5 de Dic. de 2018
Editada: madhan ravi el 6 de Dic. de 2018
Alternatively a script can contain function at the end of your script in 2016b or later
r = 3;
f = 2;
x0 = 0;
x1 = -3;
x2 = 3;
y0 = 0;
y1 = x1^2;
y2 = x2^2;
rmag = sqrt(((x1-x0)^2)+((y1-y0)^2));
fi = asind(r/rmag);
thetas = 0.25;
x = x1:0.01:x2;
y = x.^2;
theta = randi([-90,90],1,1);
xx1 = (randi(length(x),1,1));
x1 = x(xx1);
y1 = y(xx1);
x2 = (sind(theta))+ x1;
f = @(y2) sqrt(((x1-x2)^2)+((y1-y2)^2))-1;
y2 = fsolve(f,1);
m = diff(y(xx1));
x0 = [0,0];
sol = fsolve(@(k)root2d(k,m,x1,y1),x0); % function call
function F = root2d(k,m,x1,y1) % function definition
x3 = k(1);
y3 = k(2);
F=[(m*(x1-x3))-(y1-y3); %changed this line and the error was avoided
(sqrt(((y1-k(2))^2)+((x1-k(1))^2)))-1];
end

2 comentarios

Abigale Mattingly
Abigale Mattingly el 5 de Dic. de 2018
This worked, thank you so much!
madhan ravi
madhan ravi el 6 de Dic. de 2018
Editada: madhan ravi el 6 de Dic. de 2018
Anytime :)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Polynomials en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 5 de Dic. de 2018

Editada:

el 6 de Dic. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by