I wrote the following code to solve for a:
clear; clc;
EquivRatio = [0.64 0.74 0.84 0.94 1];
K = [0.371 0.447 0.456 0.383 0.302];
C_NO = [0.014 0.0179 0.0187 0.0139 0.0084];
R1 = [5.79*10^(-5) 0.00110 0.00877 0.0265 0.0235];
Tao_NO = C_NO./(4*R1);
t = 1;
fun = @(a)(1-K).*(log(1+a))-(1+K).*(log(1-a))-t./Tao_NO;
alpha = fzero(fun,0.008229);
But it always give the following error:
Operands to the and && operators must be convertible to logical scalar values.
Error in fzero (line 322) elseif ~isfinite(fx) ~isreal(fx)
What's wrong with my code?

댓글 수: 7

Diego Leal
Diego Leal 2018년 10월 8일
I seems that you are trying to find the zeros from a function that goes from scalar to vector. Is that what you want to do?
Ivy Shen
Ivy Shen 2018년 10월 8일
Hi,I want to solve for a.
Hi Ivy Shen,
As I see your code, there are 5 equations to solve a. Therefore, we should load a for loop to solve them one by one
clear; clc;
EquivRatio = [0.64 0.74 0.84 0.94 1];
K = [0.371 0.447 0.456 0.383 0.302];
C_NO = [0.014 0.0179 0.0187 0.0139 0.0084];
R1 = [5.79*10^(-5) 0.00110 0.00877 0.0265 0.0235];
Tao_NO = C_NO./(4*R1);
t = 1;
for i=1:1:length(K)
fun = @(a)(1-K(i)).*(log(1+a))-(1+K(i)).*(log(1-a))-t./Tao_NO(i);
alpha(i) = fzero(fun,0.008229);
end
I'm not sure whether i interpret your question correctly, let us know, if my recommendation is workout for you.
Ivy Shen
Ivy Shen 2018년 10월 8일
Thank you! But it gives two NaN values as shown below. Do you know what's wrong with it?
Kevin Chng
Kevin Chng 2018년 10월 8일
편집: Kevin Chng 2018년 10월 8일
You have to change 0.008229 value.
Kevin Chng
Kevin Chng 2018년 10월 8일
편집: Kevin Chng 2018년 10월 8일
for i=1:1:length(K)
fun = @(a)(1-K(i)).*(log(1+a))-(1+K(i)).*(log(1-a))-t./Tao_NO(i);
figure
fplot(fun,[-2 2])
alpha(i) = fzero(fun,0.999);
end
Hi, you may use fplot to view how is your graph's pattern. From the graph, you may see the value is leading to 1 when approaching zero for 4 & 5.
When i change the initial value to 0.999, i find the root for 4.
Ivy Shen
Ivy Shen 2018년 10월 8일
Thank you very much!

댓글을 달려면 로그인하십시오.

 채택된 답변

Kevin Chng
Kevin Chng 2018년 10월 8일

0 개 추천

Hi Ivy Shen,
As I see your code, there are 5 equations to solve a. Therefore, we should load a for loop to solve them one by one
clear; clc;
EquivRatio = [0.64 0.74 0.84 0.94 1];
K = [0.371 0.447 0.456 0.383 0.302];
C_NO = [0.014 0.0179 0.0187 0.0139 0.0084];
R1 = [5.79*10^(-5) 0.00110 0.00877 0.0265 0.0235];
Tao_NO = C_NO./(4*R1);
t = 1;
for i=1:1:length(K)
fun = @(a)(1-K(i)).*(log(1+a))-(1+K(i)).*(log(1-a))-t./Tao_NO(i);
alpha(i) = fzero(fun,0.008229);
end
you may use fplot to view how is your graph's pattern. From the graph, you may see the value is leading to 1 when approaching zero for 4 & 5.
for i=1:1:length(K)
fun = @(a)(1-K(i)).*(log(1+a))-(1+K(i)).*(log(1-a))-t./Tao_NO(i);
figure
fplot(fun,[-2 2])
alpha(i) = fzero(fun,0.999);
end

추가 답변 (0개)

제품

릴리스

R2016b

태그

질문:

2018년 10월 8일

댓글:

2018년 10월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by