Clear Filters
Clear Filters

Extracting Property Values from Property Functions

5 views (last 30 days)
I'm encountering an error in MATLAB while attempting to extract `x` and `y` values from property functions. The property functions I'm working with look like this:
- `
@(P) (subs(P, x, 2) == 0)
`
- `
@(P) (subs(P, x, 6) == 0)
`
My goal is to extract the `x` value where the condition is met and then use that value to find the corresponding `y` by substituting into a simple polynomial. However, I'm encountering the following error:
Error using isSubspacePoly>get_property_values (line 208)
No valid x value found for the given property function.
Here's the code snippet for the `get_property_values` function:
function [x_val, y_val] = get_property_values(propertyFcn)
syms x; % Symbolic variable
P = x; % Simple polynomial to apply the property function
% Get the condition from the property function
eqn = propertyFcn(P);
% Solve for x to meet the property condition
x_val = double(solve(eqn, x));
if isempty(x_val) % Handle cases with no valid solution
error('No valid x value found for the given property function.');
end
% Compute y by substituting x_val into the polynomial
y_val = double(subs(P, x, x_val));
if isempty(y_val) % Ensure valid y_val
error('No valid y value obtained from the given property function.');
end
end
The error occurs when there's no valid solution for `x_val`. If I can't get `x`, then I can't find `y`. What might be causing this? Could it be an issue with the property function or something else? Any suggestions on how to extract `x` and `y` values correctly would be helpful.

Answers (1)

SAI SRUJAN
SAI SRUJAN on 30 Apr 2024
Hi Teoman,
I understand that you are facing an issue extracting the property values from property functions.
We can see that you are having trouble with extracting 'x' and 'y' values from property functions in MATLAB, especially when 'x_val' turns out to be empty. The crux of the issue lies in the part of the code that halts execution upon encountering an empty 'x_val'. This happens because there is an 'error' statement, which stops the execution entirely when no valid 'x' value is found.
Another approach is to use a 'warning' statement instead of an 'error'. This way, even if 'x_val' is empty, the code will alert the issue but won't stop execution. Consequently, it allows the execution to continue and the code proceeds to calculate 'y_val' even when 'x_val' is empty.
Please go through the following code sample to proceed further,
function [x_val, y_val] = get_property_values(propertyFcn)
syms x; % Symbolic variable
P = x; % Simple polynomial to apply the property function
% Get the condition from the property function
eqn = propertyFcn(P);
% Solve for x to meet the property condition
x_val = double(solve(eqn, x));
if isempty(x_val) % Handle cases with no valid solution
warning('No valid x value found for the given property function.');
end
% Compute y by substituting x_val into the polynomial
y_val = double(subs(P, x, x_val));
if isempty(y_val) % Ensure valid y_val
warning('No valid y value obtained from the given property function.');
end
end
For a comprehensive understanding of the 'error and 'warning' in MATLAB, please go through the following documentation.
I hope this helps!

Products

Community Treasure Hunt

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

Start Hunting!