Analytical differentiation in for loops

6 Ansichten (letzte 30 Tage)
Peter Lynch
Peter Lynch am 6 Nov. 2024
Kommentiert: Walter Roberson am 6 Nov. 2024
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 Kommentare
Peter Lynch
Peter Lynch am 6 Nov. 2024
R2020a is that helps
Walter Roberson
Walter Roberson am 6 Nov. 2024
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?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Taylor
Taylor am 6 Nov. 2024
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

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by