MATLAB Answers

i need help with my code, for error

1 view (last 30 days)
Vicente Fuentealba Reyes
Vicente Fuentealba Reyes on 16 Nov 2020
I have an error in line 12, help pls
clc
close all
Ks=inputdlg('Ingrese valor de la rugosidad de la tuberia: ')
Re=inputdlg('Ingrese numero de Reynolds: ')
D=inputdlg('Ingrese diametro de la tuberia')
funcion=char(inputdlg('Ingrese la funcion'));
f=inline(2*log10((Ks/3.7*D)+(2.51/Re*sqrt(log10*x)))+1/sqrt(log10*x));
x=-5:0.001:30;
n=length(x);
for i=1:n
y(i)=f(x(i));
end
plot(x,t,'LineWidth',2);
hold on
plot([-5 30],[0 0],'r');
grid on;
plot([0 0],[-50 250],'r');
hold off
x1=str2double(inputdlg('ingrese el valor de x(i-1)'));
x2=str2double(inputdlg('Ingrese el valor de x(i)'));
tol=str2double(inputdlg('Ingrese error admisible en %'))
error=100;
i=1;
disp('Iter x(i-1) x(i) x(i+1) Error(%)');
hold on
while error>tol
x3=x2-(f(x2)*(x2-x1))/(f(x2)-f(x1));
error=abs((x3-x2)/x3)*100;
fprintf('%2.0f %f %f %f %f\n',i,x1,x2,x3,error)
if f(x1)>f(x2)
if f(x1)>0 && f(x2>0)
hold on
plot([x1 x3],[f(x1) 0],'m')
end
if f(x1)>0 && f(x2)<0
hold on
plot([x1 x1],[f(x1) f(x2)],'m')
end
end
if f(x1)<=f(x2)
if f(x1)>0 && f(x2)>0
hold on
plot([x3 x2],[0 f(x2) ],'m')
end
if f(x1)<0 && f(x2)>0
hold on
plot([x1 x2],[f(x1) f(x2)],'m')
end
end
x1=x2;
x2=x3;
i=i+1;
end
disp(' ')
disp('solucion: ')
disp(['x= ',num2str(x3),' en ',num2str(i-1),' iteraciones '])
plot(x3,f(x3),'go','LineWidth',2)

Answers (2)

Walter Roberson
Walter Roberson on 16 Nov 2020
That is the wrong syntax for inline(). inline() needs a character vector, not code.
Unless you will lose marks if you do not use inline specifically, then you should use an anonymous function instead of inline()
  3 Comments
Walter Roberson
Walter Roberson on 16 Nov 2020
if you build the inline incorrectly then f might not end up the correct class.

Sign in to comment.


Sulaymon Eshkabilov
Sulaymon Eshkabilov on 16 Nov 2020
Here are corrected part of your script:
Ks=str2double(doubleKs{:}); Re=str2double(Re{:}); D = str2double(D{:}); % Data conversion for calc's
f=@(x)(2*log10((Ks/3.7*D)+(2.51/Re.*sqrt(log10(x)))+1./sqrt(log10(x))));
x=-5:0.001:30;
n=length(x);
y=zeros(1,n); % Memory allocation for the efficency
for i=1:n
y(i)=f(x(i));
end
plot(x,y,'LineWidth',2); % Variable t is to be y

Community Treasure Hunt

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

Start Hunting!