Fsolve for complexe adding variable

1 回表示 (過去 30 日間)
Martin Maurel
Martin Maurel 2020 年 10 月 27 日
回答済み: Martin Maurel 2020 年 10 月 30 日
hello everybody
I tried to solve an equation , which solution is a complexe.
clc
[z, fval] = fsolve(@myfun, [1; 1] )
function F = myfun(X)
z = X(1, :) + i*X(2, :); % Create complex value from real and imaginary parts
A=4*(z) ./ ((z)).^2;
B= exp(-i.*(z).*2.128958086786507e+04.*1.300000000000000e-06);
C= ( ((z)-1)./((z)+1) ).^2;
f = A.*B./(1+C) - 0.180699042773709 + 0.075060653715432i; % Evaluate complex function
F = [real(f); imag(f)]; % Separate real and imaginary parts
end
I's working, but in B, the 2 last value are vector, B= exp(-i.*(z).* k.* R); so I need to solve it for each value of k and R.
But if I write
B= exp(-i.*(z).* k1.*1.300000000000000e-06); % k1=k(1)
Unrecognized function or variable 'k1'.
Error in test>myfun (line 15)
B= exp(-i.*(z).*k1.*1.300000000000000e-06);
Error in fsolve (line 258)
fuser = feval(funfcn{3},x,varargin{:});
Error in test (line 8)
[z, fval] = fsolve(@myfun, [1; 1] );
Caused by:
Failure in initial objective function evaluation.
FSOLVE cannot continue.
Don't understand how I can solve it
Thank you in advance.
M

回答 (2 件)

Tarunbir Gambhir
Tarunbir Gambhir 2020 年 10 月 30 日
The function "fsolve" is a Nonlinear system solver which solves a problem specified by F(x) = 0 for x, where F(x) is a function that returns a vector value. In your first code, the function solved for the vector "X" successfully.
After making the change you get an error for unrecognized function or variable "k1" because you have not declared the variable anywhere in the function body.
If you want to solve the system for different parameters "k" and "R", I suggest you use an anonymous function with "fsolve" to get solution of the system for each set of parameters.

Martin Maurel
Martin Maurel 2020 年 10 月 30 日
Thank for your answer
I try this
I know that my real part should be around 4. and my Imaginary part around 0.1 something.
But when I change X0, I am able to have what I want
clc
clear all
load('f_ref.mat','f_ref');
index_freq_min=345;
index_freq_max = 2065
load('n_ref.mat','n_ref');
load('k_ref.mat','k_ref');
lbase=508e-6;
lS=1.9e-6;
load('k.mat','k');
load('Ratio.mat','Ratio');
p=index_freq_min: 10: index_freq_max;
nR=n_ref-i.*k_ref;
nR=nR(:,1);
ff=f_ref(p);
x0 = [8,+0.01];
b=1;
for p=index_freq_min: 10: index_freq_max;
F = @(x) (8.*nR(p).*(x(1)-i*x(2))) .* ...
((1+nR(p)) .*(1+(x(1)-i*x(2))) .*(nR(p)+(x(1)-i*x(2))) ) .*...
(exp(-i.*k(p).*lS(1).*((x(1)-i*x(2))-1)) .* exp(-i.*k(p).*lbase.*(nR(p)-1)) ) ./...
(1- ((x(1)-i*x(2))-1).*((x(1)-i*x(2))-nR(p)) .* exp(-i.*4.*(x(1)-i*x(2)).*k(p).*lS(1)) ./( ((x(1)-i*x(2))+1).* ((x(1)-i*x(2))+nR(p)) ) ) - ...
Ratio(p)
xx(b,:) = fsolve(F,x0);
x0=xx(b,:)
plot(ff(1:b),real(xx(:,1)),'color','r');
hold on
plot(ff(1:b),real(xx(:,2)),'color','k');
b=b+1;
xlim([0.5 3])
drawnow
end
the equation is : n is what I try to found, it's a complexe, as Ratio, as nR
n = @(x) x(1)-i*x(2);
A=@(n) 8.*nR(p).*n;
B= @(n) (1+nR(p)) .*(1+n) .*(nR(p)+n) ;
C= @(n) exp(-i.*k(p).*lS(1).*(n-1)) .* exp(-i.*k(p).*lbase(7).*(nR(p)-1)) ;
D= @(n) (n-1).*(n-nR(p)) .* exp(i.*2.*n.*k(p).*lS(1)) ./( (n+1).* (n+nR(p)) );
eqn= @(n) 8.*nR(p).*n-Ratio(p); % thin layer
I don't know wich solver I can use now

カテゴリ

Help Center および File ExchangeSystems of Nonlinear Equations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by