segment function issues by using matlab
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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
0 comentarios
Respuestas (2)
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
0 comentarios
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)
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
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)
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
.
Ver también
Categorías
Más información sobre Resizing and Reshaping Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!