Analytical differentiation in for loops

조회 수: 1 (최근 30일)
Peter
Peter 2024년 11월 6일
댓글: Walter Roberson 2024년 11월 6일
Hello,
When I run the below with Master(i,1) replaced by Master(1,1) code runs with no errors.
Master = [1000, 2000, 5000, 10000, 20000, 50000, 100000, 200000, 500000, 1e6, 2e6, 5e6, 10e6; 100, 200, 500, 1000, 2000, 5000, 10000, 20000, 50000, 1e5, 2e5, 5e5, 10e5];
syms x1
GRs = 200;
Rss = Master(1,1)+GRs*Master(1,1)*x1;
dRs = diff(Rss, x1);
dRs2 = diff(Rss^2, x1);
dRs_val = vpa(subs(dRs,x1,0));
dRs2_val = vpa(subs(dRs2,x1,0));
As soon as I put into for loop like below:
Master = [1000, 2000, 5000, 10000, 20000, 50000, 100000, 200000, 500000, 1e6, 2e6, 5e6, 10e6; 100, 200, 500, 1000, 2000, 5000, 10000, 20000, 50000, 1e5, 2e5, 5e5, 10e5];
for i = 1:length(Master)
syms x1
GRs = 200;
Rss = Master(i,1)+GRs*Master(i,1)*x1;
dRs = diff(Rss, x1);
dRs2 = diff(Rss^2, x1);
dRs_val = vpa(subs(dRs,x1,0));
dRs2_val = vpa(subs(dRs2,x1,0));
end
I get the following error: NaN/Inf breakpoint hit for symvar.m on line 33.
33 n = inf;
Even after clearing workspace usinf the diff function on simple expressions copied from ttps://www.mathworks.com/help/symbolic/differentiation.html return same error. Restart fixes this issue.
Where am I going wrong?
  댓글 수: 5
Peter
Peter 2024년 11월 6일
R2020a is that helps
Walter Roberson
Walter Roberson 2024년 11월 6일
I do not happen to have R2020a installed, but I checked R2019b and R2020b and symvar.m in them does not have any assignment of inf
Is it possible that you have Maple installed?

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

채택된 답변

Taylor
Taylor 2024년 11월 6일
You have an indexing issue. Master is a 2x13 array. By indexing to Master(i,1) you are exceeding the index bounds once you go past i=2. Change Master(i,1) to Master(1,i) and the code will run.
Master = [1000, 2000, 5000, 10000, 20000, 50000, 100000, 200000, 500000, 1e6, 2e6, 5e6, 10e6; 100, 200, 500, 1000, 2000, 5000, 10000, 20000, 50000, 1e5, 2e5, 5e5, 10e5];
for i = 1:length(Master)
syms x1
GRs = 200;
Rss = Master(1,i)+GRs*Master(1,i)*x1;
dRs = diff(Rss, x1);
dRs2 = diff(Rss^2, x1);
dRs_val = vpa(subs(dRs,x1,0));
dRs2_val = vpa(subs(dRs2,x1,0))
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by