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

Community Tip - Did you get an answer that solved your problem? Please mark it as an Accepted Solution so others with the same problem can find the answer easily. X

How to find maximum for 2 variable function

MySchizoBuddy
1-Newbie

How to find maximum for 2 variable function

My elevation function depends of day of year (d) and decimal hours of the day(hrs)

I want to find Solar Noon the point of highest elevation angle.

I have done it for just one day of the year. But i want it to be able to calculate solar noon for any day of the year. I want it to be a variable of d as well

Attaching the image for help

Thank you in advance you guys have really helped a lot in there.

1 ACCEPTED SOLUTION

Accepted Solutions

You've been yelled at for not posting the sheet; we're all lazy and hate typing.

In the image you posted there's a solve block. Above the given (under the X in the image) you define a guess variable, and the condition in the solve block has an argument with two parameters, "Today" and "hrs." I'm assuming from the output of the solve block that hrs is the guess variable. Rewrite the "Find" statement to be:

LocalSolarNoon(Today):=Find(hrs)

Now your solve block is a function. When you write "LocalSolarNoon(xxx) =" it will compute the solve block for watever value you've put in for xxx.

View solution in original post

31 REPLIES 31

Minimizing for two variables is always a hard task. For the nature of your problem probably the best approach could be eval z=f(x,y) over a rectangular grid (x,y) and search for min in the matrix z.

What you can do with partial derivatives is search the critical points, but numerically they are that, points, when the analysis shows that they usually are spatial lines with some isolated points too.

Regards. Alvaro.

how can i reduce the dimensionality

A(1,hrs) to B1(hrs)

A(2.hrs) to B2(hrs)

then i only want to find the maximum of B1(hrs) and B2(hrs) to B365(hrs) save all that in an array

Discretize (this is the usual matlab method):

n.day = 1

n.hrs = 199

n.r = 0..n.day

n.c = 0..n.hrs

This is: a grid 2x200

days[n.r = n.r[n.r

hrs[n.c = n.c[n.c

M[n.r,n.c = A(days[n.r , hrs[n.c)

Now you have a matrix of values in the grid. Can search for max or min with ... max and min functions, and can take where the max or min occurs with match.

Regards. Alvaro.

Posting the actual sheet would be much more useful if you really want help. Not that many people have the patience to re-type portions of a sheet, with unknown numbers of missing parameters.

But, in your particular case, your assignment of UTCSolarNoon to the find(hrs) should be made into a function ala UTCSolarNoon(day):=find(hrs). You then create a vector of days of the year and feed it to the function, and get back a vector solar noons.

TTFN

MikeArmstrong
5-Regular Member
(To:IRstuff)

Posting the actual sheet would be much more useful if you really want help. Not that many people have the patience to re-type portions of a sheet, with unknown numbers of missing parameters.

We seem to say this all the time.

In my personnel experience, I feel more willing to help when a worksheet is included.

Mike

then you are doing the work for me.

I guess you guys don't mind that. so here is the worksheet. scroll 3/4 way down till you reach the yellow box. I have explained what i want to do

Thanks in advance.

You've been yelled at for not posting the sheet; we're all lazy and hate typing.

In the image you posted there's a solve block. Above the given (under the X in the image) you define a guess variable, and the condition in the solve block has an argument with two parameters, "Today" and "hrs." I'm assuming from the output of the solve block that hrs is the guess variable. Rewrite the "Find" statement to be:

LocalSolarNoon(Today):=Find(hrs)

Now your solve block is a function. When you write "LocalSolarNoon(xxx) =" it will compute the solve block for watever value you've put in for xxx.

thats it.

it works.

I guess i over complicated the problem myself.

There seems to be an issue with your worksheet when opening in M14, see attached image.

Also, your equation goes red when I get down to the hrs.min(d) function - You seem to be calling UTLSolarNoon as a function without making the solve block a function.

Mike

On another note you have the function FillVec(x) in there and its not doing anything???

Have a look at the attached sheet, seems to work now.

Mike

almost everything is fixed and working now

I upload M11 cause everybody said thats what i should post. I don't have M11 i have M14.

perhaps the conversion to M11 is creating problems.

the fillVec is to be used instead of the range variables.

my d and hrs should be vectors and not range variables. that will speed things up. but i couldn't get the fillVec to work

I have modified you FillVec(x) function in my sheet and it now works. Usually I would recommend posting in M11 format, I will edit my post now.

If you have a look at the attached sheet there are a few user defined functions which create vector ranges. There are many more examples within the, but these few seem sufficient enough for my needs.

Cheers

Mike

trying it right now

the SolarNoon calculation is extremely slow. cause i'm finding the elevation angle maximum for every day of the year.

Can it be speed up

File attached as requested. I have marked the problem areas.

here is the M14 file

just a couple of things not working

fillVec and hhmmss units on the y-axis and solarNoon optimization

A few notes added in the attached worksheet.

Mike

FillVec was not designed to take a scalar argument. It was designed to take a range, just as you would use for a range variable. I have no idea how it got corrupted so that it didn't work.

MikeArmstrong
5-Regular Member
(To:RichardJ)

FillVec was not designed to take a scalar argument. It was designed to take a range, just as you would use for a range variable. I have no idea how it got corrupted so that it didn't work.

My mistake, sorry.

He's got mulitple methods of creating a vector now.

Mike

MikeArmstrong
5-Regular Member
(To:RichardJ)

And it works with units.......Problem is that it only works with sequential values, but then again the range variables must have been created with sequential values in the first place.

Mike

Hello Ziyad,

Hope this helps.

If you want to speed up the calculation, you have to be very carefull when using functions to define your equations.

Mathcad will call the appropriate function everytime for each point that you ask it to (even if it is passing the same parameters and getting the identical answer).

Where ever possible vectorise the calculation and store (constant) intermediate results in vector arrays.

The calculations of each point required is then done once and the only time overhead is recalling the value.

I've made a couple of changes to the worksheet and added a timer.

(Open the sheet & scroll to the end to ensure the sheet recalculates everything)

On your original the time was ~130 sec

with mods it reduced to ~70

I'm sure that it should be possible to reduce this further.

Regards

Andy

the day of the year variable (d) is already an integer so it can be used as an index.

The decimal hour (hrs=0,0.01;24) isn't an integer. If i multiply it by 100 so it is (0,1;2400) then i can have decimal hours being used as an index.

then lot of these functions are simply 2D array. huge arrays though 2400 x 365. Will this be worth while.

will have to divide by 100 later on when plotting.

if you change d and hrs from a range to a vector using FillVec, how will the LiAux and JD function look like then

I'm sure that it should be possible to reduce this further.

I need to rewrite the SplitDate function so that it handles vectors of dates.

Maybe tomorrow.

and the Num2Date script so it can handle vector of numbers

Yes, I was going to do both. I didn't find time today though. Tomorrow hopefully.

Like this?

Num2script.gif

Mike

Num2Date can generate the entire year in an instance.

i tried up to 3 years without seeing the yellow bulb.

WWWWAAAAAHHHHHHOOOO.

Please you got it sorted.

Mike

Not so fast!

Sorry. I was getting confused! Date2Num and Num2Date are written to take vectors. You do not even need to vectorize.

AssembleDate and SplitDate are not written to take vectors, and vectorizing them will not speed things up. What is currently needed is a new version of SplitDate that takes a vector of dates and outputs a matrix with the pieces of each date in the columns, but I will aslo write a new version of AssembleDate that takes a matrix as input and outputs a vector of dates.

Top Tags