Can you update a constant in a function.

16 Ansichten (letzte 30 Tage)
Lee Luderman
Lee Luderman am 22 Jan. 2018
Kommentiert: Kevin am 7 Okt. 2021
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 Kommentare
Adam
Adam am 22 Jan. 2018
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.
Lee Luderman
Lee Luderman am 22 Jan. 2018
How would I go about defining this property across multiple function files?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 22 Jan. 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 Kommentar
Lee Luderman
Lee Luderman am 22 Jan. 2018
Thank you for the prompt reply, what options are there to achieve what I'm looking to do?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Guillaume
Guillaume am 22 Jan. 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 Kommentare
Guillaume
Guillaume am 22 Jan. 2018
Bearbeitet: Guillaume am 22 Jan. 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.
Kevin
Kevin am 7 Okt. 2021
Thank you so much for your explanation, this problem has troubled me for a long time.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Data Type Identification finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by