cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

The community will undergo maintenance on October 16th at 10:00 PM PDT and will be unavailable for up to one hour.

Scalar value error with matrix work

BG_7818704
4-Participant

Scalar value error with matrix work

I am trying to work on a convergence program with a certain loop in mind. I am trying to calculate/converge on a solution for a range of values, about 20, rather than typing each in by hand. I've defined the matrix with the given numbers. However, one value needed for the majority is throwing a fit by saying "value must be scalar". It was doing this earlier when I wasn't calling the correct arguments, but I don't see the current error. I've attached the sheet and screenshot of the error, any help would be appreciated.

BG_7818704_0-1615411846161.png

 

4 REPLIES 4
LucMeekes
23-Emerald III
(To:BG_7818704)

Your sheet does not match the picture.

In the picture you define M as a vector, instead of a scalar.

This makes Tt and Pt also vectors. Then you call Gamma, where you supply Tt as a parameter.

Internally in Gamma, Tt is represented by T. Now this T gets compared with 0 and 300 in the first IF statement.

Remember that now T=Tt is a vector. You cannot compare a vector with a scalar.

If you want to run this with multiple values for M automatically, you'll have to make your algorithm deal with that.

 

Success!
Luc

BG_7818704
4-Participant
(To:LucMeekes)

Is there a particular route you would recommend in this situation to fix this?

 

Typically, if you have a function that works using a scalar argument, you can use it to evaluate each element of a vector by "vectorizing" it:

Fred_Kohlhepp_0-1615482112402.png

 

Werner_E
25-Diamond I
(To:BG_7818704)

Vectorization is a powerful operator.

Why not write a function to make the whole iteration and then later call that function vectorized!

Werner_E_1-1615508714279.png

 

When calling the function with a vector M, the first function argument, the initial value for gamma, must be either a single scalar (as the 1.3 in the example above) or a vector the same size of M (so every value in M has its own initial gamma value).

Instead of returning gamma in the last program line (which is only executed when after maxiter iterations there still no satisfyíng value was found) you may consider returning a string which says something about the calculation not converging or replace that line with an appropriate error(..) statement.

 

 

Announcements

Top Tags