Analytical differentiation in for loops

59 次查看(过去 30 天)
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 个评论
Walter Roberson
Walter Roberson 2024-11-6,21:12
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,14:44
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