필터 지우기
필터 지우기

i need help with my code, for error

조회 수: 2 (최근 30일)
Vicente Fuentealba Reyes
Vicente Fuentealba Reyes 2020년 11월 16일
답변: Sulaymon Eshkabilov 2020년 11월 16일
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)

답변 (2개)

Walter Roberson
Walter Roberson 2020년 11월 16일
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
Vicente Fuentealba Reyes
Vicente Fuentealba Reyes 2020년 11월 16일
error in for cycle, y(i)
Walter Roberson
Walter Roberson 2020년 11월 16일
if you build the inline incorrectly then f might not end up the correct class.

댓글을 달려면 로그인하십시오.


Sulaymon Eshkabilov
Sulaymon Eshkabilov 2020년 11월 16일
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

카테고리

Help CenterFile Exchange에서 String에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by