segment function issues by using matlab

3 visualizaciones (últimos 30 días)
Xuejian Niu
Xuejian Niu el 31 de En. de 2022
Respondida: Image Analyst el 31 de En. de 2022
This is a segment function by solving in Matlab, but I have no idea why do Matlab keeps show me errors....
% Define values: a, b, and chord length
a1 = 0.6;b1 = 0.1;
% Define number of points
x1 = linspace(0,3*a,181);
y = up(x1,a1,b1)
function s = up(x,a,b)
z = zeros(size(x));
k1 = find(x > 0 & x <= a);
yu1 = ((b^2 - (b^2/a^2)*(x - a).^2)).^1/2;
k2 = find(x > a & x < 3*a);
yu2 = ((b/(4*a^3)).*x.^3) - (3*b/2*a^2).*x.^2 + (9*b/4*a).*x;
end

Respuestas (2)

Image Analyst
Image Analyst el 31 de En. de 2022
Lots of errors. Anyway, look this over and try it:
% Define values: a, b, and chord length
a = 0.6;
b = 0.1;
% Define number of points
x = linspace(0, 3*a, 181);
y = up(x, a, b)
function [yu] = up(xAll,a,b)
yu = zeros(size(xAll));
for k = 1 : length(yu)
x = xAll(k);
if x > 0 && x <= a
% x is in the range [0, a]
yu(k) = ((b^2 - (b^2/a^2)*(x - a).^2)).^1/2;
elseif x > a && x < 3*a
% x is in the range [a, 3*a]
yu(k) = ((b/(4*a^3)).*x.^3) - (3*b/2*a^2).*x.^2 + (9*b/4*a).*x;
else
% x is outside the range [0, 3a]
yu(k) = -1; % Error flag.
end
end
end

Star Strider
Star Strider el 31 de En. de 2022
The error is that ‘s’ was not defined anywhere in the ‘up’ function, so MATLAB has nothing to assign to it in order to return it as the function output.
Also, the find calls are not necessary since ‘logical indexing’ would be more efficient. However the results (‘k1’ and ‘k2’) are not used anywhere either.
% Define values: a, b, and chord length
a1 = 0.6;b1 = 0.1;
% Define number of points
x1 = linspace(0,3*a1,181);
y = up(x1,a1,b1)
Output argument "s" (and possibly others) not assigned a value in the execution with "solution>up" function.
function s = up(x,a,b)
z = zeros(size(x));
k1 = find(x > 0 & x <= a);
yu1 = ((b^2 - (b^2/a^2)*(x - a).^2)).^1/2;
k2 = find(x > a & x < 3*a);
yu2 = ((b/(4*a^3)).*x.^3) - (3*b/2*a^2).*x.^2 + (9*b/4*a).*x;
end
.
  2 comentarios
Xuejian Niu
Xuejian Niu el 31 de En. de 2022
Editada: Image Analyst el 31 de En. de 2022
Thanks for answering my question. After fixing it, it still keeps showing me the same issue as you mentioned before, so how can I correct it?
% Define values: a, b, and chord length
a = 0.6;b = 0.1;
% Define number of points
x = linspace(0,3*a,181);
y = up(x,a,b)
function [yu] = up(x,a,b)
zeros(size(x));
if x > 0 & x <= a;
yu = ((b^2 - (b^2/a^2)*(x - a).^2)).^1/2;
elseif x > a & x < 3*a;
yu = ((b/(4*a^3)).*x.^3) - (3*b/2*a^2).*x.^2 + (9*b/4*a).*x;
end
end
Star Strider
Star Strider el 31 de En. de 2022
I replaced the if blocks with ‘logical indexing’ vectors, and changed ‘<’ in the second test in ‘Lv2’ to ‘<=’ so that it would consider the entire vector. I also changed the zeros call so that it preallocates ‘yu’.
There still appear to be problems in the calculation, however the logic problems are no longer present. (I added the plot just to see what the function does.)
% Define values: a, b, and chord length
a = 0.6;b = 0.1;
% Define number of points
x = linspace(0,3*a,181);
y = up(x,a,b)
y = 1×181
0 0.0002 0.0003 0.0005 0.0006 0.0008 0.0009 0.0011 0.0012 0.0014 0.0015 0.0017 0.0018 0.0019 0.0021 0.0022 0.0023 0.0024 0.0026 0.0027 0.0028 0.0029 0.0030 0.0031 0.0032 0.0033 0.0034 0.0035 0.0036 0.0037
figure
plot(x, y)
grid
function [yu] = up(x,a,b)
yu = zeros(size(x));
Lv1 = x > 0 & x <= a;
yu(Lv1) = ((b^2 - (b^2/a^2)*(x(Lv1) - a).^2)).^1/2;
Lv2 = x > a & x <= 3*a;
yu(Lv2) = ((b/(4*a^3)).*x(Lv2).^3) - (3*b/2*a^2).*x(Lv2).^2 + (9*b/4*a).*x(Lv2);
end
.

Iniciar sesión para comentar.

Categorías

Más información sobre Resizing and Reshaping Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by