Solving a System of Nonlinear Equations n Times With Different Values
Hi, I am trying to solve a system of nonlinear equations n times, and recording the output values for each time. For simplicity I wrote less complex, random nonlinear equations here rather than what I was working with, but it is the same idea. I have tried a number of things, and this skeleton of code has been the closest, but I cannot get it to return different values for each iteration. I found that I cannot embed a function into a large for loop because of MATLAB syntax, and placing the for loop within the function returns nothing. I believe that fsolve only solves the system of equations for z1(10) and z2(10) and not the first 9 values in the vectors. I can’t find any information on how to change this. Any help would be greatly appreciated.
x0 = [1,1];
m = 10; % same length as z1 below, because I want to output 10 values
c = 0; % counting value
for q=1:m
x = fsolve(@g,x0)
c = c+1;
p(:,c) = x; % records each output value
end
function F = g(x)
z1 = [5 6 7 8 9 10 11 12 13 14];
z2 = [1 2 3 4 5 6 7 8 9 10];
n = length(z1)
for i=1:n
F(1) = x(1).*x(2) – z1(i)
F(2) = x(1) + x(2)./x(1) – z2(i)
end
endHi, I am trying to solve a system of nonlinear equations n times, and recording the output values for each time. For simplicity I wrote less complex, random nonlinear equations here rather than what I was working with, but it is the same idea. I have tried a number of things, and this skeleton of code has been the closest, but I cannot get it to return different values for each iteration. I found that I cannot embed a function into a large for loop because of MATLAB syntax, and placing the for loop within the function returns nothing. I believe that fsolve only solves the system of equations for z1(10) and z2(10) and not the first 9 values in the vectors. I can’t find any information on how to change this. Any help would be greatly appreciated.
x0 = [1,1];
m = 10; % same length as z1 below, because I want to output 10 values
c = 0; % counting value
for q=1:m
x = fsolve(@g,x0)
c = c+1;
p(:,c) = x; % records each output value
end
function F = g(x)
z1 = [5 6 7 8 9 10 11 12 13 14];
z2 = [1 2 3 4 5 6 7 8 9 10];
n = length(z1)
for i=1:n
F(1) = x(1).*x(2) – z1(i)
F(2) = x(1) + x(2)./x(1) – z2(i)
end
end Hi, I am trying to solve a system of nonlinear equations n times, and recording the output values for each time. For simplicity I wrote less complex, random nonlinear equations here rather than what I was working with, but it is the same idea. I have tried a number of things, and this skeleton of code has been the closest, but I cannot get it to return different values for each iteration. I found that I cannot embed a function into a large for loop because of MATLAB syntax, and placing the for loop within the function returns nothing. I believe that fsolve only solves the system of equations for z1(10) and z2(10) and not the first 9 values in the vectors. I can’t find any information on how to change this. Any help would be greatly appreciated.
x0 = [1,1];
m = 10; % same length as z1 below, because I want to output 10 values
c = 0; % counting value
for q=1:m
x = fsolve(@g,x0)
c = c+1;
p(:,c) = x; % records each output value
end
function F = g(x)
z1 = [5 6 7 8 9 10 11 12 13 14];
z2 = [1 2 3 4 5 6 7 8 9 10];
n = length(z1)
for i=1:n
F(1) = x(1).*x(2) – z1(i)
F(2) = x(1) + x(2)./x(1) – z2(i)
end
end nonlinear system, fsolve MATLAB Answers — New Questions