I have two equations with two variables each is a function of another, so I'm using ''fsolve'' for solving two nonlinear equations. but if I have an equation with a specific constant which I want to give it a range (e.g. 1:50)

function F = root2d(x,n)
F(1)= x(1)-1+((1-x(2))^n);
F(2)= (x(2)*(33*(1-2*x(1))+32*x(1)*(1-(2*x(1))^3)))-2*(1-(2*x(1)));
%%%%%%%
I want to obtain the values of two variables x(1),x(2) at each value of n which takes a range (i.e. 1--->50).
How can I do that?

 Akzeptierte Antwort

Hi,
i guess you are searching for this (rename the outer function or save it as some_stuff_with_n.m in your matlab search path:
function some_stuff_with_n
sol = zeros(50,2);
for k = 1:50
n = k;
sol(k,1:2) = fsolve(@root2d,[1, 1]);
end
function F = root2d(x,~)
F(1) = x(1)-1+((1-x(2))^n);
F(2) = (x(2)*(33*(1-2*x(1))+32*x(1)*(1-(2*x(1))^3)))-2*(1-(2*x(1)));
end
end
This calculates the function root2d 50x for n=1...50, calculates the x(1) and x(2) values corresponding to this n and saves it in the variable you want. Use:
sol = some_stuff_with_n
to call it.
Best regards
Stephan

7 Kommentare

Very thanks for your help.But how I can obtain x(1),x(2) values? because I want to calculate a final parameter S that is a function of x(2) and n,so we can have the values of S corresponding to each x(2),n,i.e. having 50 value for S. How can I obtain x(1),x(2),and corresponding S values?
x(1) and x(2) are stored in sol(:,1) and sol(:,2). Every row index corresponds to the Value of n. In column 1 there is x(1) and in column 2 there is x(2).
I know that, the problem is that when running this code,I do not get sol() in workspace. Just 'the eq. is solved' appears. And when I call the function, the error 'too many arguments' appears
Change the first line to
function sol = some_stuff_with_n
Accept the answer if your question has been solved.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Parallel Computing Toolbox finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by