iterate fsolve and save each iterated result

hi every body. im using fsolve to solve 3x3 non linear system. i have a parameter (velocity) that i want to encrease every time i solve the 3x3 system. my question is, how i iterate fsolve and how i save the results. i have this main file that call the fsolve
clc
clear all
basico;
uo2=1;
yo2=0.1;
yo3=0.1;
x0 = [uo2; yo2; yo3]; % Make a starting guess at the solution
options=optimset('Display','iter');% Option to display output
[x,fval] = fsolve(@func,x0,options) % Call solver
so every time i solve the system i get a vector x with 3 values solved by fsolve. i want to iterate FSOLVE and in each iteration increase a parameter inside FUNC file and save the results for plot it.
any idea? thanks for advance =)

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 26 Mär. 2013

0 Stimmen

options=optimset('Display','iter');% Option to display output
x0 = [uo2; yo2; yo3]; % Make a starting guess at the solution
maxiter = 1000; %however many
x = zeros(1, maxiter);
fval = zeros(1, maxiter);
for ITER = 1 : maxiter %however many
newparam = revise_parameter(ITER, maxiter); %a function to return the revised parameter
[x(ITER), fval(ITER)] = fsolve(@(x) func(x, newparam), x0, options);
x0 = x(ITER);
end
plot(x, fval);
This would involve adding the "parameter inside FUNC file" as an argument to func, and would involve creating a new function revise_parameter that accepts the current iteration number and maximum iteration limit and returns the value that you would otherwise have revised the "parameter inside FUNC file" to.

5 Kommentare

MEXICO
MEXICO am 27 Mär. 2013
Bearbeitet: MEXICO am 27 Mär. 2013
thanks for the answer!!!. i tried this:
clc
options=optimset('Display','iter');% Option to display output
ao=-5;
bo=-5;
k=100;
uamax=1;
uamin=10;
dua=(uamax-uamin)/k;
x0 = [ao; bo;]; % Make a starting guess at the solution
x = zeros(1, k);
fval = zeros(1, k);
for n = 1 : k %however many
ua=(n*dua)+uamin; %a function to return the revised parameter
[x(n), fval(n)] = fsolve(@(x) myfun(x,ua), x0, options);
x0 = x(n);
end
plot(x, fval);
but i get this error msg
??? In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in ==> main at 19 [x(n), fval(n)] = fsolve(@(x) myfun(x,ua), x0, options);
but the idea is clear, i want to change the value of "ua" each iteration and save each "x" results
=)
You have a logical problem: you want to plot fval against x, but you are solving for two x values. You should probably be plotting the two x and the fval against ua.
x = zeros(k, 2); %note switch to columns
fval = zeros(k, 1); %switched to column
ua = zeros(k, 1);
for n = 1 : k
ua(n) = (n*dua) + uamin;
[x(n,:), fval(n)] = fsolve(@(x) myfun(x,ua), x0, options);
x0 = x(n,:);
end
plot(ua, [fval, x]); %plot makes one line for each column of y
MEXICO
MEXICO am 28 Mär. 2013
thanks for your answer, now im getting results!!!, i have a question with "x0 = x(n,:);" every iteration i will have new starting guesses?
Walter Roberson
Walter Roberson am 28 Mär. 2013
The starting guess for iteration #n will be the output from iteration #(n-1)
i have this
for n = 2:k;
ua(1)=uamin;
ua(n) =ua(n-1)+dua;
[x((n-1),:)] = fsolve(@(x) func(x,ua(n-1)), x0, options)
x0 = x((n-1),:);
end

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by