MATLAB Answers

Error solving for a particular variable from two equations

조회 수: 1(최근 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개)

Community Treasure Hunt

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

Start Hunting!

Translated by