how can i correct this code and get rid of error? urgent help please

조회 수: 1 (최근 30일)
Ilkay Köse
Ilkay Köse 2022년 1월 22일
댓글: Image Analyst 2022년 1월 22일
fprintf('\n The equation by Lagrange is:\n');
yvalue
fprintf('\n The answer for part c\n');
fprintf('The value of Volume at 1.05 bar by Lagrange is %f m^3\n',vpa(subs(yvalue,xvalue,x)));
fprintf('\n The answer for part d\n');
fprintf('\n The absolute difference between volume by predicted model and Lagrange interpolation is %f m^3\n',abs((A(1)*exp(A(2)*x))-(vpa(subs(yvalue,xvalue,x)))));
end
i wrote this code for my homework but gives error in bold underlined line. how can i correct it. i use Matlab R2013a . thank you.
  댓글 수: 3
Ilkay Köse
Ilkay Köse 2022년 1월 22일
this is my full code:
function [a0,a1]=work(x,xvector,fvector)
fun=@(X,xdata) X(1)*exp(X(2)*xdata);
X0=rand(1,2);
A=lsqcurvefit(fun,X0,xvector,fvector);
fprintf('\n The answer for part a\n');
fprintf('\n V=%fe^(%f*P) \n',A(1),A(2));
fprintf('\n The answer for part b\n');
fprintf('The value of Volume at 2.5 bar by model is %f m^3\n',A(1)*exp(A(2)*x));
%Lagrange Method
syms xvalue
yvalue=0;
for i=1:length(xvector)
p=1;
for j=1:length(xvector)
if i~=j
p=p*((xvalue-xvector(j))/(xvector(i)-xvector(j)));
end
end
yvalue=yvalue+(p*fvector(i));
end
fprintf('\n The equation by Lagrange is:\n');
yvalue
fprintf('\n The answer for part c\n');
fprintf('The value of Volume at 1.05 bar by Lagrange is %f m^3\n',vpa(subs(yvalue,xvalue,x)));
fprintf('\n The answer for part d\n');
fprintf('\n The absolute difference between volume by predicted model and Lagrange interpolation is %f m^3\n',abs((A(1)*exp(A(2)*x))-(vpa(subs(yvalue,xvalue,x)))));
end
and this is the error:
Error using fprintf
Function is not defined for 'sym' inputs.
Error in work (line 24)
fprintf('The value of Volume at 1.05 bar by Lagrange is %1.05f
m^3\n',vpa(subs(yvalue,xvalue,x)));

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

답변 (1개)

Simon Chan
Simon Chan 2022년 1월 22일
You need to specify the number of digits after the decimal point.
For example, %.2f or %.4f for 2 and 4 digits after the decimal point respectively.
%f states nothing and give you an error
  댓글 수: 1
Ilkay Köse
Ilkay Köse 2022년 1월 22일
still gives error. this is my full code:
function [a0,a1]=work(x,xvector,fvector)
fun=@(X,xdata) X(1)*exp(X(2)*xdata);
X0=rand(1,2);
A=lsqcurvefit(fun,X0,xvector,fvector);
fprintf('\n The answer for part a\n');
fprintf('\n V=%fe^(%f*P) \n',A(1),A(2));
fprintf('\n The answer for part b\n');
fprintf('The value of Volume at 2.5 bar by model is %f m^3\n',A(1)*exp(A(2)*x));
%Lagrange Method
syms xvalue
yvalue=0;
for i=1:length(xvector)
p=1;
for j=1:length(xvector)
if i~=j
p=p*((xvalue-xvector(j))/(xvector(i)-xvector(j)));
end
end
yvalue=yvalue+(p*fvector(i));
end
fprintf('\n The equation by Lagrange is:\n');
yvalue
fprintf('\n The answer for part c\n');
fprintf('The value of Volume at 1.05 bar by Lagrange is %f m^3\n',vpa(subs(yvalue,xvalue,x)));
fprintf('\n The answer for part d\n');
fprintf('\n The absolute difference between volume by predicted model and Lagrange interpolation is %f m^3\n',abs((A(1)*exp(A(2)*x))-(vpa(subs(yvalue,xvalue,x)))));
end

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

카테고리

Help CenterFile Exchange에서 Newton-Raphson Method에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by