Analytical differentiation in for loops

59 views (last 30 days)
Peter Lynch
Peter Lynch on 6 Nov 2024 at 13:30
Commented: Walter Roberson on 6 Nov 2024 at 21:12
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 Comments
Walter Roberson
Walter Roberson on 6 Nov 2024 at 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?

Sign in to comment.

Accepted Answer

Taylor
Taylor on 6 Nov 2024 at 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

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!