Community Tip - Need to share some code when posting a question or reply? Make sure to use the "Insert code sample" menu option. Learn more! X
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.
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
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:
Vectorization is a powerful operator.
Why not write a function to make the whole iteration and then later call that function vectorized!
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.