Linspace giving me undefined function 'uminus'

7 ビュー (過去 30 日間)
Jessica Tolentino
Jessica Tolentino 2020 年 12 月 2 日
コメント済み: Stephen23 2020 年 12 月 3 日
classdef PatchApp < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
InputsPanel matlab.ui.container.Panel
InterracialtoughnessNm32Label matlab.ui.control.Label
InterracialToughness matlab.ui.control.NumericEditField
Initialvolumeofairtrappedincrackm3Label matlab.ui.control.Label
InitialVolume matlab.ui.control.NumericEditField
RadiusofstalkmLabel matlab.ui.control.Label
RadiusStalk matlab.ui.control.NumericEditField
ElasticModulusPaLabel matlab.ui.control.Label
ElasticModulus matlab.ui.control.NumericEditField
NumberofiterationofsecantmethodLabel matlab.ui.control.Label
NumIterations matlab.ui.control.NumericEditField
OutputPanel matlab.ui.container.Panel
MaximumCriticalStressEditField matlab.ui.control.NumericEditField
MaximumCriticalStressNm2Label matlab.ui.control.Label
Switch matlab.ui.control.Switch
AtmosphericPressurePaLabel matlab.ui.control.Label
CalculateButton matlab.ui.control.Button
InitialcracklengthmLabel matlab.ui.control.Label
InitialCrackLength matlab.ui.control.NumericEditField
UIAxes matlab.ui.control.UIAxes
end
%
properties (Access = private)
% Kc
% V0
% b
% a0
% E
% m
P0
%
% Sc
end
%
%
methods (Access = private)
function results = SecantFunc(~,x1,x2,f,e)
i = x1;
j = x2;
fj = Inf;
%loop until residual falls below error bound
while abs(fj) > e
%fbp is derivative of fp
fjp = (f(j) - f(i)) ./ (j - i);
i = j;
%Secant method
j = j - (f(j) ./ fjp);
%evaluate function (residual at current guess)
%.*1 to get 1x1
fj = f(j) .* 1;
results = j;
end
end
function results = discretedatadiff(~,x,y)
%Size of array
n = length(x);
d = zeros(1,n);
%Forward Difference Approximation
d(1) = (y(2) - y(1)) / (x(2) - x(1));
%Central Difference Approximation
for i = 2: n-1
d(i) = (y(i+1) - y(i-1)) / (x(i+1) - x(i-1));
end
%Backwards Difference Approximation
results(n) = (y(n) - y(n-1)) / (x(n) - x(n-1));
end
function results = lagrange(~,x,y,xt)
%Get length of matrices
n = length(xt);
p = length(y);
%Initialize output array yf
results = ones(1,n);
%Iterate through array of xt
for k = 1:n
%Initialize fx and L matrix
%this ensures that L and fx gets reset after for every k
L = ones(1,p);
fx = 0;
%Lagrange interpolation
for i = 1:p
for j = 1:p
%iterate j from 1 to n except when i = j
if i ~= j
%cumulative product
L(i) = ((xt(k) - x(j))/ (x(i) - x(j))) * L(i);
end
end
%cumulative sum or summation of products of Li(x)*yi
%fx is running total
fx = (L(i) * y(i)) + fx;
f(i) = fx;
end
results(k) = f(p);
end
end
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: CalculateButton
function CalculateButtonPushed(app, event)
Kc = app.InterracialToughness;
V0 = app.InitialVolume;
b = app.RadiusStalk;
E = app.ElasticModulus;
m = app.NumIterations;
a0 = app.InitialCrackLength;
%Let Sc=x
%initial guesses
SwitchValueChanged(app);
x1=app.P0;
x2=0.5*app.P0;
%array of equally spaced crack radii over range of m
a=linspace(a0,b,m);
%setting up approximate solutions as an array
xa=zeros(size(a));
%convergence criterion - approximate error
e=0.000000001;
%Equation (2)
%V/V0=0.5+(4*P0*b^3/3*E*V0)*((Sc/P0)-1)*(a0/b)^3*g+0.5*sqrt((1+(8*P0*b^3/3*E*V0)*((Sc/P0)-1)*(a0/b)^3*g)^2+(32*P0*b^3/3*E*V0)*(a0/b)^3*g);
for i=1:m
%Equation (4)
h=(1-0.5*(a(i)/b)+0.148*(a(i)/b)^3)/(sqrt(1-(a(i)/b)));
%Equation (3)
g=(1/(a(i)/b)^3)*(1.260*log(1/(1-(a(i)/b)))-1.260*(a(i)/b)-0.630*(a(i)/b)^2+0.580*(a(i)/b)^3-0.315*(a(i)/b)^4-0.102*(a(i)/b)^5+0.063*(a(i)/b)^6+0.093*(a(i)/b)^7-0.0081*(a(i)/b)^8);
%Equation (2) into (1)
f=@(x) (2/pi)*((x/app.P0)+(1/(0.5+(4*app.P0*b^3/(3*E*V0))*((x/app.P0)-1)*(a(i)/b)^3*g+0.5*sqrt((1+(8*app.P0*b^3/(3*E*V0))*((x/app.P0)-1)*(a(i)/b)^3*g)^2+(32*app.P0*b^3/(3*E*V0))*(a(i)/b)^3*g)))-1)*sqrt(a(i)/b)*h-(Kc/(app.P0*sqrt(pi*b)));
end
%storing array of variables, a and xa, into new variables, x and y,
%respectively
x=a;
y=xa;
switch app.Switch.Value
case 'Reduced'
%FOR UNSTABLE
xmax=y(1);
amax=a0;
case 'Earth'
%Numerical differentiation to approximate array of 'dxa/da' values using finite difference approx.
d = discretedatadiff(app,x,y);
%Finding the indices of the two data points at which 'dxa/da' crosses from
%being positive to negative
for i=1:m
if d(i)>0
indexpos=i; %positve
indexneg=i+1; %negative
end
end
%linear Lagrange interpolation between the indices of the two data
%points for 'dxa/da' and 'a' which exhibit sign change
x=[d(indexpos), d(indexneg)];
y=[a(indexpos), a(indexneg)];
xt=0; % 'dxa/da' = 0
%crack radius maximum
yt=lagrange(app,x,y,xt);
amax=yt;
%non-linear equation solver for critical stress maximum approximation using corresponding crack radius, 'amax'
%Equation (4)
h=(1-0.5*(amax/b)+0.148*(amax/b)^3)/(sqrt(1-(amax/b)));
%Equation (3)
g=(1/(amax/b)^3)*(1.260*log(1/(1-(amax/b)))-1.260*(amax/b)-0.630*(amax/b)^2+0.580*(amax/b)^3-0.315*(amax/b)^4-0.102*(amax/b)^5+0.063*(amax/b)^6+0.093*(amax/b)^7-0.0081*(amax/b)^8);
%Equation (2) into (1)
f=@(x) (2/pi)*((x/P0)+(1/(0.5+(4*P0*b^3/(3*E*V0))*((x/P0)-1)*(amax/b)^3*g+0.5*sqrt((1+(8*P0*b^3/(3*E*V0))*((x/P0)-1)*(amax/b)^3*g)^2+(32*P0*b^3/(3*E*V0))*(amax/b)^3*g)))-1)*sqrt(amax/b)*h-(Kc/(P0*sqrt(pi*b)));
%initial guesses
x1=xa(indexpos);
x2=xa(indexneg);
%critical stress maximum approximation
xmax=SecantFunc(app,x1,x2,f,e);
end
app.MaximumCriticalStressEditField.Value = xmax;
%PLOT
hold(app.UIAxes);
plot(app.UIAxes,a,xa);
plot(app.UIAxes,amax,xmax,'k-X');
end
% Value changed function: Switch
function SwitchValueChanged(app, event)
value = app.Switch.Value;
switch value
case 'Reduced'
P0 = 1*10^-3;
case 'Earth'
P0 = 101*10^3;
end
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Position = [100 100 903 482];
app.UIFigure.Name = 'MATLAB App';
% Create InputsPanel
app.InputsPanel = uipanel(app.UIFigure);
app.InputsPanel.Title = 'Inputs';
app.InputsPanel.Position = [23 32 376 438];
% Create InterracialtoughnessNm32Label
app.InterracialtoughnessNm32Label = uilabel(app.InputsPanel);
app.InterracialtoughnessNm32Label.HorizontalAlignment = 'right';
app.InterracialtoughnessNm32Label.Position = [65 380 175 22];
app.InterracialtoughnessNm32Label.Text = 'Interracial toughness [N/ m^3/2]';
% Create InterracialToughness
app.InterracialToughness = uieditfield(app.InputsPanel, 'numeric');
app.InterracialToughness.Position = [255 374 87 34];
app.InterracialToughness.Value = 1;
% Create Initialvolumeofairtrappedincrackm3Label
app.Initialvolumeofairtrappedincrackm3Label = uilabel(app.InputsPanel);
app.Initialvolumeofairtrappedincrackm3Label.HorizontalAlignment = 'right';
app.Initialvolumeofairtrappedincrackm3Label.Position = [11 333 229 48];
app.Initialvolumeofairtrappedincrackm3Label.Text = 'Initial volume of air trapped in crack [m^3]';
% Create InitialVolume
app.InitialVolume = uieditfield(app.InputsPanel, 'numeric');
app.InitialVolume.ValueDisplayFormat = '%e';
app.InitialVolume.Position = [255 333 87 34];
app.InitialVolume.Value = 1e-15;
% Create RadiusofstalkmLabel
app.RadiusofstalkmLabel = uilabel(app.InputsPanel);
app.RadiusofstalkmLabel.HorizontalAlignment = 'right';
app.RadiusofstalkmLabel.Position = [135 218 104 22];
app.RadiusofstalkmLabel.Text = 'Radius of stalk [m]';
% Create RadiusStalk
app.RadiusStalk = uieditfield(app.InputsPanel, 'numeric');
app.RadiusStalk.ValueDisplayFormat = '%e';
app.RadiusStalk.Position = [254 212 87 34];
app.RadiusStalk.Value = 0.0002;
% Create ElasticModulusPaLabel
app.ElasticModulusPaLabel = uilabel(app.InputsPanel);
app.ElasticModulusPaLabel.HorizontalAlignment = 'right';
app.ElasticModulusPaLabel.Position = [125 176 114 22];
app.ElasticModulusPaLabel.Text = 'Elastic Modulus [Pa]';
% Create ElasticModulus
app.ElasticModulus = uieditfield(app.InputsPanel, 'numeric');
app.ElasticModulus.ValueDisplayFormat = '%e';
app.ElasticModulus.Position = [255 170 87 34];
app.ElasticModulus.Value = 1000000;
% Create NumberofiterationofsecantmethodLabel
app.NumberofiterationofsecantmethodLabel = uilabel(app.InputsPanel);
app.NumberofiterationofsecantmethodLabel.HorizontalAlignment = 'right';
app.NumberofiterationofsecantmethodLabel.Position = [35 140 204 22];
app.NumberofiterationofsecantmethodLabel.Text = 'Number of iteration of secant method';
% Create NumIterations
app.NumIterations = uieditfield(app.InputsPanel, 'numeric');
app.NumIterations.ValueDisplayFormat = '%.3f';
app.NumIterations.Position = [254 128 87 34];
app.NumIterations.Value = 100;
% Create OutputPanel
app.OutputPanel = uipanel(app.InputsPanel);
app.OutputPanel.Title = 'Output';
app.OutputPanel.Position = [8 1 352 73];
% Create MaximumCriticalStressEditField
app.MaximumCriticalStressEditField = uieditfield(app.OutputPanel, 'numeric');
app.MaximumCriticalStressEditField.Editable = 'off';
app.MaximumCriticalStressEditField.Position = [249 12 87 37];
% Create MaximumCriticalStressNm2Label
app.MaximumCriticalStressNm2Label = uilabel(app.OutputPanel);
app.MaximumCriticalStressNm2Label.Position = [70 16 179 32];
app.MaximumCriticalStressNm2Label.Text = 'Maximum Critical Stress [N/m^2]';
% Create Switch
app.Switch = uiswitch(app.InputsPanel, 'slider');
app.Switch.Items = {'Reduced', 'Earth'};
app.Switch.ValueChangedFcn = createCallbackFcn(app, @SwitchValueChanged, true);
app.Switch.Position = [268 296 38 17];
app.Switch.Value = 'Reduced';
% Create AtmosphericPressurePaLabel
app.AtmosphericPressurePaLabel = uilabel(app.InputsPanel);
app.AtmosphericPressurePaLabel.Position = [53 293 147 22];
app.AtmosphericPressurePaLabel.Text = 'Atmospheric Pressure [Pa]:';
% Create CalculateButton
app.CalculateButton = uibutton(app.InputsPanel, 'push');
app.CalculateButton.ButtonPushedFcn = createCallbackFcn(app, @CalculateButtonPushed, true);
app.CalculateButton.Position = [120 84 127 32];
app.CalculateButton.Text = 'Calculate ';
% Create InitialcracklengthmLabel
app.InitialcracklengthmLabel = uilabel(app.InputsPanel);
app.InitialcracklengthmLabel.HorizontalAlignment = 'right';
app.InitialcracklengthmLabel.Position = [117 260 122 22];
app.InitialcracklengthmLabel.Text = 'Initial crack length [m]';
% Create InitialCrackLength
app.InitialCrackLength = uieditfield(app.InputsPanel, 'numeric');
app.InitialCrackLength.ValueDisplayFormat = '%e';
app.InitialCrackLength.Position = [254 254 87 34];
app.InitialCrackLength.Value = 1e-05;
% Create UIAxes
app.UIAxes = uiaxes(app.UIFigure);
title(app.UIAxes, 'Atmospheric Pressure')
xlabel(app.UIAxes, 'Crack radius [m]')
ylabel(app.UIAxes, 'Critical Stress [N/m^2]')
zlabel(app.UIAxes, 'Z')
app.UIAxes.CameraPosition = [0.5 0.5 9.16025403784439];
app.UIAxes.CameraViewAngle = 7.3797277859937;
app.UIAxes.DataAspectRatio = [1 1 1];
app.UIAxes.PlotBoxAspectRatio = [1 1 1];
app.UIAxes.Position = [418 61 450 390];
% Show the figure after all components are created
app.UIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = PatchApp
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end
[SL: formatted the code as code. In the future, Jessica Tolentino, please highlight your code and press the first button in the Code section of the toolbar to make the code easier to read (by highlighting it as it would have been highlighted in the Editor.)]
  4 件のコメント
Jessica Tolentino
Jessica Tolentino 2020 年 12 月 2 日
I've tried doing several things on the line 140 that keeps giving me errors and here is what it's showing me. I'm using MATLAB R2020b
Line 140 a=a0:(b-a0)/(m-1):b; %ERROR: Undefined function 'minus' for input arguments of type 'matlab.ui.control.NumericEditField'.
Line 140 a=linspace(a0,b,m); %ERROR: Undefined function 'uminus' for input arguments of type 'matlab.ui.control.NumericEditField'.
Error in linspace (line 25)
if d1 == -d2 && n > 2 && isfloat(d1) && isfloat(d2)

サインインしてコメントする。

採用された回答

Steven Lord
Steven Lord 2020 年 12 月 2 日
Here's what appears to be the relevant section of code.
b = app.RadiusStalk;
E = app.ElasticModulus;
m = app.NumIterations;
a0 = app.InitialCrackLength;
%Let Sc=x
%initial guesses
SwitchValueChanged(app);
x1=app.P0;
x2=0.5*app.P0;
%array of equally spaced crack radii over range of m
a=linspace(a0,b,m);
You're passing the handles of the NumericEditField objects in your app into linspace. You want to pass the values stored in those NumericEditField objects into linspace instead.
a0 = app.InitialCrackLength.Value;
b = app.RadiusStalk.Value;
m = app.NumIterations.Value;
a = linspace(a0, b, m);
  2 件のコメント
Jessica Tolentino
Jessica Tolentino 2020 年 12 月 2 日
that works ! thank u so much but now i keep getting this error on line 190 which I don't understand since indexpos and indexneg is just a standalone variable that i only use within that function:
Unrecognized function or variable 'indexpos'.
Steven Lord
Steven Lord 2020 年 12 月 3 日
for i=1:m
if d(i)>0
indexpos=i; %positve
indexneg=i+1; %negative
end
end
If none of the elements of d are positive, the body of the if statement never executes and the variables indexpos and indexneg never get created.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDevelop uifigure-Based Apps についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by