Skip to main content
10-Marble
October 22, 2025
Solved

I'm having trouble with the "for" loop

  • October 22, 2025
  • 2 replies
  • 555 views

Hi dear, I'm having trouble with the "for" loop when I create a function and try to graph a spectrum as a function of time.
I have MathCAD version 11. It worked in the old version, but in this latest version, it shows me an error message stating that the variable T is not defined.
Why is this happening?

pannus0_0-1761138052930.png

 

Best answer by Werner_E

It worked in the old version, but in this latest version, it shows me an error message stating that the variable T is not defined.
Why is this happening?

 


What you did worked OK up to version 9 of Prime.

You are using an undocumented trick which turned a range into a vector - the inline evaluation (the = at the end) did the trick.

Werner_E_0-1761145158061.png

So up to Prime 9  variable T was now a vector, but starting with Prime 10 this variable still is just a range and that's the reason your sheet is failing.

As Alan already said  you have to define "T" as a vector and there are many ways you could do so.

The easiest way is to use the new function "vec" (it was introduced with Prime 11).

Simply replace your definition of "T" with   T:=vec(0s,0.01s,Tmax and your sheet should again be up and running.

 

But actually I wonder why you would go through the hassle of using vectors at all. You are using a math program, not just a spreadsheet like Excel. So there is no need to manipulate vectors - you could rather use functions to do the job - much more versatile:

Werner_E_2-1761146925167.png

 

 

 

2 replies

19-Tanzanite
October 22, 2025

You have constructed T as a range variable.  It needs to be a vector.

As I only have the Express version I've done your worksheet without the programming structure.  However, your loop should work ok if you make the change from range variable to vector.

 

Please upload your worksheet next time - not just an image.

 

Spectrum.png

pannus010-MarbleAuthor
10-Marble
October 29, 2025

Hi AlanStevens 

sorry for not responding sooner, as I was unwell, I have , I have some questions regarding the use of i:1...500, Ti:i/500*Tmax. Why did you use this method? Could you explain?
Thanks

25-Diamond I
October 29, 2025

Its another way to create a vector T.

Note that Alan's sheet uses ORIGIN=0.

It seems from your picture that you are suing ORIGIN=1.

So to create the same vector T as you would using   T:=vec(0s,0.01s,Tmax)    you could use

Werner_E_0-1761767997182.png

or 

Werner_E_1-1761768049100.png

The "vec" function is not available in Prime 10 (and below) and furthermore its a 'Premium' function and so not available in the free Express version which was used by Alan.

Werner_E25-Diamond IAnswer
25-Diamond I
October 22, 2025

It worked in the old version, but in this latest version, it shows me an error message stating that the variable T is not defined.
Why is this happening?

 


What you did worked OK up to version 9 of Prime.

You are using an undocumented trick which turned a range into a vector - the inline evaluation (the = at the end) did the trick.

Werner_E_0-1761145158061.png

So up to Prime 9  variable T was now a vector, but starting with Prime 10 this variable still is just a range and that's the reason your sheet is failing.

As Alan already said  you have to define "T" as a vector and there are many ways you could do so.

The easiest way is to use the new function "vec" (it was introduced with Prime 11).

Simply replace your definition of "T" with   T:=vec(0s,0.01s,Tmax and your sheet should again be up and running.

 

But actually I wonder why you would go through the hassle of using vectors at all. You are using a math program, not just a spreadsheet like Excel. So there is no need to manipulate vectors - you could rather use functions to do the job - much more versatile:

Werner_E_2-1761146925167.png

 

 

 

pannus010-MarbleAuthor
10-Marble
October 29, 2025

Hi Werner_E,

sorry for not responding sooner, as I was unwell, but your answer was the most viable.