Can you update a constant in a function.
Mostra commenti meno recenti
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
Adam
il 22 Gen 2018
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.
Lee Luderman
il 22 Gen 2018
Adam
il 22 Gen 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
il 22 Gen 2018
Risposta accettata
Più risposte (1)
Guillaume
il 22 Gen 2018
1 voto
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
Lee Luderman
il 22 Gen 2018
Guillaume
il 22 Gen 2018
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.
Lee Luderman
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.
Kevin
il 7 Ott 2021
Thank you so much for your explanation, this problem has troubled me for a long time.
Categorie
Scopri di più su Data Type Identification in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!