equation solver solve()
显示 更早的评论
Hi all,
I use eqn solver for solving non-linear equation. ı have an array with constants and for the value Vg = -5.24, soln must be Zero but I got an error:
Error using subsasgn
Subscripted assignment dimension mismatch.
Error in sym/privsubsasgn (line 997)
L_tilde2 = builtin('subsasgn',L_tilde,struct('type','()','subs',{varargin}),R_tilde);
Error in sym/subsasgn (line 834)
C = privsubsasgn(L,R,inds{:});
Error in onebytwo (line 25)
Sol(i) = vpasolve(Vs == Vg(i) + 5.24 - 1.8996*(exp(-Vs/0.026)+ Vs/0.026 -1 + 8.65*10^-14*(exp(Vs/0.026) - Vs/0.026 -1))^0.5, Vs,
guess)
Also below -5.24 ( -5.2,-5.0 ... etc) code works fine but does not solve Vg with more than two values. For this ı got an error like ;
Error using subsasgn
In an assignment A(:) = B, the number of elements in A and B must be the same.
Error in sym/privsubsasgn (line 997)
L_tilde2 = builtin('subsasgn',L_tilde,struct('type','()','subs',{varargin}),R_tilde);
Error in sym/subsasgn (line 834)
C = privsubsasgn(L,R,inds{:});
Error in onebytwo (line 22)
Sol(i) = vpasolve(Vs == Vg(i) + 5.24 - 1.8996*(exp(-Vs/0.026)+ Vs/0.026 -1 + 8.65*10^-14*(exp(Vs/0.026) - Vs/0.026 -1))^0.5, Vs,
guess)
Moreover, Code does not solve values of Vg above -5.24 (-5.4,-5.6 ... etc)
Here is my code.
clear all
syms Vs Vg
guess = 0.01;
Vg = [
-5.4
-5.24
-5.2
-5.0
-4.8
-4.6
-4.4
];
for i = 1:length(Vg)
Sol(i) = vpasolve(Vs == Vg(i) + 5.24 - 1.8996*(exp(-Vs/0.026)+ Vs/0.026 -1 + 8.65*10^-14*(exp(Vs/0.026) - Vs/0.026 -1))^0.5, Vs, guess)
if ~isempty(Sol); guess = Sol; end
end
Hope I can get help.
Thank you
Hasan
回答(1 个)
Walter Roberson
2021-2-14
编辑:Walter Roberson
2021-2-14
0 个投票
I clearly showed in https://www.mathworks.com/matlabcentral/answers/739187-eqn-solver-could-not-solve-my-specific-seqn#comment_1322742 how to deal with that problem of subscript mismatch, with the thissol approach and testing for empty before assigning into the array.
12 个评论
Hasan canar
2021-2-14
Walter Roberson
2021-2-14
So? This question is about subscript mismatches, and I already showed you how to solve that problem, days ago.
Hasan canar
2021-2-14
编辑:Hasan canar
2021-2-14
Hasan canar
2021-2-14
编辑:Hasan canar
2021-2-14
Walter Roberson
2021-2-15
Sol(i) = vpasolve(Vs == Vg(i) + 5.24 - 1.8996*(exp(-Vs/0.026)+ Vs/0.026 -1 + 8.65*10^-14*(exp(Vs/0.026) - Vs/0.026 -1))^0.5, Vs, guess)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Extract the underlined part:
- 1.8996*(exp(-Vs/0.026)+ Vs/0.026 -1 + 8.65*10^-14*(exp(Vs/0.026) - Vs/0.026 -1))^0.5
What does that look like? Well, if you draw it close to 0, you will see that lt looks like it has a pointy local maxima of -4/25 at Vs = 0. Does that check with calculus? Take the derivative and plot it near zero and you will see a discontinuity for sure at 0, with the derivative being about +50 for negative Vs and about -50 for positive Vs. If you break up the derivative further there is a part that is a division by 0 at Vs = 0
So.. the maximum value of that big sub-expression is -4/25.
Now look at the expresson before that:
Vs == Vg(i) + 5.24 + (something that is at most -4/25)
and let Vg(i) be -5.24 . Then that -5.24 Vg(i) balances the 5.24 and gives you
Vs == 0 + (something that is at most -4/25)
That hints maybe Vs could be -4/25, but the -4/25 was when Vs = 0, and near Vs = -4/25 the term is closer to -40
Effectively in order to find a zero, you have to count on the Vg(i) + 5.24 being positive to balance out the negative of the longer expression, and that cannot happen when Vg(i) < -5.24 (or at -5.24 either.)
So... unless you are willing to accept complex-valued solutions, you are not going to get any further, except perhaps to pin down the exact boundary slightly greater than -5.24.
In earlier discussion, you said that the solutions should be real-valued, so I will not bother investigating the families of complex-valued solutions .
At this point I would suggest you have another look at how the 5.24 got into your original formula.
Hasan canar
2021-2-15
You can modify the initial guess further. Just do not use 0 as the imaginary component for the initial guess, or else vpasolve() will not look for imaginary solutions.
syms VsR VsI real
Vg = [
-5.4
-5.24
-5.2
-5.0
-4.8
-4.6
-4.4
];
Q = @(v) sym(v);
numVg = length(Vg);
Sol = zeros(numVg, 3, 'sym');
Vs = VsR + 1i*VsI;
guess = [-0.01; -2];
for i = 1:length(Vg)
eqn = Vs == Vg(i) + Q(5.24) - Q(1.8996)*(exp(-Vs/Q(0.026))+ Vs/Q(0.026) -1 + Q(8.65)*10^-14*(exp(Vs/Q(0.026)) - Vs/Q(0.026) -1))^Q(0.5);
eqn_sep = [real(lhs(eqn))==real(rhs(eqn)), imag(lhs(eqn)) == imag(rhs(eqn))];
thissol = vpasolve(eqn_sep, [VsR, VsI], guess);
if isempty(thissol) || isempty(thissol.VsR)
Sol(i,:) = [sym(Vg(i),'d'), nan, nan];
else
thissolsep = [thissol.VsR, thissol.VsI];
Sol(i,:) = [sym(Vg(i),'d'), thissolsep];
guess = thissolsep;
end
end
Sol
Hasan canar
2021-2-16
Hasan canar
2021-2-16
编辑:Hasan canar
2021-2-16
syms VsR VsI real
Vg = [
-5.4
-5.35
-5.3
-5.25
-5.24
-5.2
-5.0
-4.8
-4.6
-4.4
];
Q = @(v) sym(v);
numVg = length(Vg);
Sol = zeros(numVg, 3, 'sym');
Vs = VsR + 1i*VsI;
guess = [-0.01; -20];
for i = 1:length(Vg)
eqn = Vs == Vg(i) + Q(5.24) - Q(1.8996)*(exp(-Vs/Q(0.026))+ Vs/Q(0.026) -1 + Q(8.65)*10^-14*(exp(Vs/Q(0.026)) - Vs/Q(0.026) -1))^Q(0.5);
eqn_sep = [real(lhs(eqn))==real(rhs(eqn)), imag(lhs(eqn)) == imag(rhs(eqn))];
thissol = vpasolve(eqn_sep, [VsR, VsI], guess);
if isempty(thissol) || isempty(thissol.VsR)
Sol(i,:) = [sym(Vg(i),'d'), nan, nan];
else
thissolsep = [thissol.VsR, thissol.VsI];
Sol(i,:) = [sym(Vg(i),'d'), thissolsep];
guess = thissolsep;
end
end
vpa(Sol,16)
Hasan canar
2021-2-16
编辑:Hasan canar
2021-2-16
Hasan canar
2021-2-16
编辑:Hasan canar
2021-2-16
类别
在 帮助中心 和 File Exchange 中查找有关 Mathematics 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



