Nested functions procedure error

Hi,
I created some nested function but it reports the error. In the first function, I calculated values for temperatures x1 and x2. Then, I defined function result that represents thermostat which should determine should thermostat be on or off in each iteration. In case that thermostat does not work in each iteration, temperature raise. Here are the functions:
function xprime = heat_sys(t,x);
params...
xprime = (system of diff equat)
function result = thermostat(x);
on = true;
off = false;
for i=1:n(end)
if(x(i,1)>=18 && x(i,1)<=20)
result(i)=on;
else if(x(i,1)<18)
result(i)=on;
else if(x(i,1)>20)
result(i,1)=off;
end;
end;
end;
end;
By the way, is this right approach to do this or I could use something different also?
Thanks in advance!

8 Comments

What is the error you get?
Maybe I expressed wrong. It is not really error but I got values that are so high. That indicate that thermostat function didn't act in each iteration. My goal is to keep x values within bounds given in if else statement.
How do you call the two functions?
I called only heat_sys function since thermostat function is nested inside. I want to check these temperature conditions in each iteration in order to prevent temperature to raise.
Nesting a function does not cause the function to be called. MATLAB would not know what arguments to call the function on, or what variable to write the output to. You need to have an explicit call to thermostat in your code.
Nikola
Nikola on 9 Sep 2013
Edited: Nikola on 9 Sep 2013
Aha ok. Thus, how I suppose to write a procedure which execute that? What should be change in existed procedure that I wrote here?
The xprime you are returning does not look to involve values calculated by thermostat(). If it actually does, then you would have the calls in the way you defined your system of equations... e.g.,
xprime = [ .... t^5+thermostat(x), ....]
If, though, the differential equation matrix is not calculated in terms of thermostat calls, then some other layer needs to receive the result of the thermostat calls. What layer is that?
I think that I also thought about the same thing today but I couldn't figure a way how to solve the system of equations separately and then relate to the thermostat function.
Thermostat input should be xprime output, correct? Is that answer to your question?

Sign in to comment.

 Accepted Answer

Walter Roberson
Walter Roberson on 9 Sep 2013
I am going to guess that you have missed out on telling us that your heat_sys function is being invoked by ode45 or similar. I am going to further guess that heat_sys is your main function, and that your thermostat function is needed to calculate non-linear constraints. If I am correct, then a handle to your thermostat function would need to be passed as an additional parameter to the ode call, and it would also have to be modified to return information for both equality constraints and inequality constraints (I cannot tell which you intend in your existing code.)
If I am correct so far, please have another look at the documentation for the ode function you are using.

3 Comments

Right, I used ode45 to solve heat_sys function and it is main function because I want to obtain x1 and x2 values. Thermostat is necessary to check if x values are within given bounds and should be switch on or off.
To handle thermostat function into ode, is this correct: [t,x] = ode45(@heat_sys,@thermostat,x0,tspan)
Since I am not an experienced Matlab user, I need a help to make corrections in procedure that I wrote above.
Okay so how do the thermostat values actually get used? What takes them as inputs ? Or are they purely constraints (which it doesn't quite sound to me that they are) ?
Output thermostat values are on and off. It means if temperature is below 18, then is on. Also, I want to keep temperature between 18 and 20 but if goes above 20, then thermostat should be switched off. Therefore, inputs of thermostat function are output values of xprime because in every another iteration, we have new value of xprime, or x1 and x2.

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!