Error solving for a particular variable from two equations

2 ビュー (過去 30 日間)
Jonas Freiheit
Jonas Freiheit 2021 年 10 月 5 日
回答済み: Alan Stevens 2021 年 10 月 5 日
Hi all,
I am having trouble with solving for a particular variable 'a' from two equations, I end up with an error saying :
Array indices must be
positive integers or
logical values.
Error in sym/privsubsasgn
(line 1124)
L_tilde2 =
builtin('subsasgn',L_tilde,struct('type','()','subs',{varargin}),R_tilde);
Error in sym/subsasgn
(line 961)
C =
privsubsasgn(L,R,inds{:});
Error in assignment3 (line
10)
sig_on_sigo_real(TonTc)=sig_on_sigo_2;
From the following code:
clc, clear, clear all
kB=-1.381*10^-16;
mu_H=9.274*10^-21;
J_value=0.5
syms a %Should I do this?
for TonTc=.01:0.01:.99
sig_sigo_1=((2*J_value+1)/2*J_value)*coth((2*J_value+1)/2*J_value)*a-(1/2*J_value)*coth(a/2*J_value); %First equation
sig_on_sigo_2=((J_value+1)/3*J_value)*TonTc*a; %second equation
solve(sig_sigo_1, sig_on_sigo_2); %Trying to solve for a
sig_on_sigo_real(TonTc)=sig_on_sigo_2; %subbing in the value of a I obtained from the line before
end
What should I do to properly solve for 'a'?
Cheers

採用された回答

Alan Stevens
Alan Stevens 2021 年 10 月 5 日
You could use fzero:
TonTc = 0.01:0.01:0.99;
a = zeros(1,numel(TonTc));
a0 = 1; % initial giuess
for k = 1:numel(a)
a(k) = fzero(@(a) fn(a,TonTc(k)),a0);
end
plot(TonTc,a,'o'),grid
xlabel('TonTc'),ylabel('a')
function z = fn(a, TonTc)
J_value = 0.5;
sig_sigo_1=((2*J_value+1)/2*J_value)*coth((2*J_value+1)/2*J_value)*a-(1/2*J_value)*coth(a/2*J_value);
sig_on_sigo_2=((J_value+1)/3*J_value)*TonTc*a;
z = sig_sigo_1 - sig_on_sigo_2;
end

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeConversion Between Symbolic and Numeric についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by