Can you update a constant in a function.

I currently have 3 functions which rely on a constant I have created inside a file called cConstants.m
classdef cConstants
properties (Constant)
a = 3
m = 1
end
end
The 3 functions which this constant are myv.m, mydv.m and myd2v.m
function [ v ] = myv( x )
import constants.*;
v = 2*exp(-(x^2)/2)+(cConstants.m/(cConstants.m+(x-cConstants.a)^2));
end
With mydv.m and myd2v.m being the first and second derivative of myv.m
I have a function which finds the minimum points of myv.m.
I now want to create a function which finds the minimum points of myv.m as the constant cConstants.a varies over an interval.
Something similar to this.
function f = PlotMin(a,b,h) %%a = starting value of a, b = end value of a and h is increase interval
hold on
while a <= b
cConstants.a = a;
x = FindMin(-10,10,0.5,4);
y = mydv( x );
plot(x,y)
a = a+h;
end

4 Commenti

If you could change it it wouldn't be a constant. That sounds like a flippant answer, but it is a simple case of something that does exactly what it says. If you want to change it then make it a non-constant property.
I see, the reason why I used the constant was it was the only way I could find to set a value for a symbol across different function files. I didn't think about the fact I would need to alter it for the graph. Thank you for the reply.
You can set a default value for any property so if you do that and never change it then it would act like a constant from a usage perspective, but it gives the additional ability to alter it if you need to, which can be good and bad since it allows for accidental changes that a constant doesn't, but works for what you want with deliberate changes.
How would I go about defining this property across multiple function files?

Accedi per commentare.

 Risposta accettata

Walter Roberson
Walter Roberson il 22 Gen 2018
"Use constant properties to define constant values that you can access by name. Create a class with constant properties by declaring the Constant attribute in the property blocks. Setting the Constant attribute means that, once initialized to the value specified in the property block, the value cannot be changed."

1 Commento

Thank you for the prompt reply, what options are there to achieve what I'm looking to do?

Accedi per commentare.

Più risposte (1)

Guillaume
Guillaume il 22 Gen 2018
By definition a constant property is ... constant. Matlab is not designed so that these properties can change.
To be perfectly honest, it is possible to design the class so that you could change the constant properties but that would involve having to call clear classes each time you want to update the constant values. This could have some unpleasant side effects.
The workaround would be to create a singleton non-constant class. You'd be in effect creating a class that acts as a global so I'm not sure it is a good idea either.
I think you'd be better off having a normal class with non-constant properties and pass instances of the class to your mydv and co. functions.

5 Commenti

Thank you for the reply, How would I go about updating the class within the function?
This is what I would recommend:
classdef Coefficients
properties
a = 3;
m = 1;
end
end
function v = myv(x, coeffs)
v = 2*exp(-(x^2)/2)+(coeffs.m/(coeffs.m+(x-coeffs.a)^2));
end
function f = PlotMin(a,b,h) %%a = starting value of a, b = end value of a and h is increase interval
coeffs = Coefficients;
hold on
while a <= b
coeffs.a = a;
x = FindMin(-10,10,0.5,4);
y = mydv(x, coeffs);
plot(x,y)
a = a+h;
end
end
But to be honest, you may as well use a structure instead of a class for the above.
----
If you really want to have a class that acts as a global:
classdef Coefficients < handle
properties
a = 3;
m = 1;
end
methods (Static)
function singleton = getinstance %function to retrieve the singleton
persistent singleton;
if isempty(singleton)
singleton = Coefficients;
end
end
end
methods (Access = private) %private constructor. Can only be invoked by getinstance
function this = Coefficients
end
end
end
function v = myv(x)
coeffs = Coefficients.getinstance;
v = 2*exp(-(x^2)/2)+(coeffs.m/(coeffs.m+(x-coeffs.a)^2));
end
function f = PlotMin(a,b,h) %%a = starting value of a, b = end value of a and h is increase interval
coeffs = Coefficients.getinstance;
hold on
while a <= b
coeffs.a = a;
x = FindMin(-10,10,0.5,4);
y = mydv(x);
plot(x,y)
a = a+h;
end
end
But this suffers the same problem as any global: Any code, anywhere, can change the value of the coefficients under your feet.
Thank you for taking the time to answer.
In the end I ended up creating two functions.
function SetGlobalm(val)
global m
m = val;
function r = getGlobalm
global m
r = m;
For both a and m
Then called them in my myv function like so.
function [ v ] = myv( x )
a = getGlobala;
m = getGlobalm;
v = 2*exp(-(x^2)/2)+(m/(m+(x-a)^2));
end
Then was able to edit them in my final plot function like so.
function f = PlotMin(u,v,h) %%a = starting value of a, b = end value of a and h is increase interval
SetGlobalm(1);
a = getGlobala;
m = getGlobalm;
hold on
g = zeros((v-u+1)/h, 2);
while u <= v
SetGlobala(u);
y = FindMin(-10,10,0.5,4);
x = u;
plot (x,y,':r*')
y = myd2v(FindMin(-10,10,0.5,4))/m;
plot (x,y,':b*')
u = u+h;
end
Guillaume
Guillaume il 22 Gen 2018
Modificato: Guillaume il 22 Gen 2018
The getGlobalx functions are a bit pointless. They don't give any kind of safety above just fetching the global directly.
Using globals (or my singleton class emulating globals) is really not recommended (in any language). It really makes it hard to debug code when any function anywhere can change the value of a variable. It's a maintenance nightmare when you have to review every single line of code in every function when you edit a global variable and have to check which piece of code it will affect.
The proper way to do what you want is to pass these not really constant anymore values to the function, as proper arguments.
Thank you so much for your explanation, this problem has troubled me for a long time.

Accedi per commentare.

Categorie

Community Treasure Hunt

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

Start Hunting!

Translated by