the code gives error. can you help me to correct it?. I really do need help!!

1 次查看(过去 30 天)
this description includes 3 things for you to understand clearly
1) the code
2) the error
3) the question
1)
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 1.05 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
after I saved this, I typed my command window:
work(1.05,[0.664 0.985 1.108 1.363 1.631 1.934 2.356 3.1],[32 25 22.2 18 15 12 9 7.2])
2) The output is like this:
>> work(1.05,[0.664 0.985 1.108 1.363 1.631 1.934 2.356 3.1],[32 25 22.2 18 15 12 9 7.2])
Local minimum possible.
lsqcurvefit stopped because the final change in the sum of squares relative to
its initial value is less than the default value of the function tolerance.
<stopping criteria details>
The answer for part a
V=51.100434e^(-0.734771*P)
The answer for part b
The value of Volume at 1.05 bar by model is 23.624488 m^3
The equation by Lagrange is:
yvalue =
(36*((125*xvalue)/93 - 19/6)*((125*xvalue)/249 - 277/498)*((200*xvalue)/423 - 197/423)*((250*xvalue)/609 - 166/609)*((500*xvalue)/583 - 967/583)*((1000*xvalue)/1469 - 1631/1469)*((1000*xvalue)/1737 - 1363/1737))/5 - 9*((125*xvalue)/93 - 25/6)*((125*xvalue)/156 - 277/312)*((250*xvalue)/423 - 166/423)*((500*xvalue)/211 - 967/211)*((40*xvalue)/29 - 1631/725)*((1000*xvalue)/993 - 1363/993)*((1000*xvalue)/1371 - 985/1371) - 32*((250*xvalue)/111 - 277/111)*((250*xvalue)/423 - 589/423)*((100*xvalue)/127 - 967/635)*((250*xvalue)/609 - 775/609)*((1000*xvalue)/321 - 985/321)*((1000*xvalue)/699 - 1363/699)*((1000*xvalue)/967 - 1631/967) - (111*((250*xvalue)/111 - 166/111)*((125*xvalue)/156 - 589/312)*((125*xvalue)/249 - 775/498)*((200*xvalue)/51 - 1363/255)*((1000*xvalue)/123 - 985/123)*((500*xvalue)/413 - 967/413)*((1000*xvalue)/523 - 1631/523))/5 + 12*((100*xvalue)/127 - 332/635)*((500*xvalue)/413 - 554/413)*((500*xvalue)/211 - 1178/211)*((500*xvalue)/583 - 1550/583)*((1000*xvalue)/303 - 1631/303)*((1000*xvalue)/571 - 1363/571)*((1000*xvalue)/949 - 985/949) + 25*((200*xvalue)/423 - 620/423)*((1000*xvalue)/321 - 664/321)*((1000*xvalue)/123 - 1108/123)*((500*xvalue)/189 - 1363/378)*((500*xvalue)/323 - 1631/646)*((1000*xvalue)/949 - 1934/949)*((1000*xvalue)/1371 - 2356/1371) - 15*((250*xvalue)/67 - 1363/268)*((500*xvalue)/323 - 985/646)*((40*xvalue)/29 - 2356/725)*((1000*xvalue)/523 - 1108/523)*((1000*xvalue)/303 - 1934/303)*((1000*xvalue)/967 - 664/967)*((1000*xvalue)/1469 - 3100/1469) + 18*((200*xvalue)/51 - 1108/255)*((500*xvalue)/189 - 985/378)*((250*xvalue)/67 - 1631/268)*((1000*xvalue)/699 - 664/699)*((1000*xvalue)/571 - 1934/571)*((1000*xvalue)/993 - 2356/993)*((1000*xvalue)/1737 - 3100/1737)
The answer for part c
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 %f
m^3\n',vpa(subs(yvalue,xvalue,x)));
3)
Here is the question that I wrote the code for:
Create the model by using least square method and find a 2nd degree equation. after that find the root when xinitial= 0 ( where interceps 0) by Newton Raphson method. then a code should be created to calculate predicted volume when pressure is 1.05 bar from the equation. Lagrange interpolation is applicated for 1.05 bar.
At last, absolute difference should be calculated between the volume calculated using interpolation and the volume calculated using the model.
Here is the question. Please solve it making sure it is true. Thanks. AND DO NOT COPY PASTE
Use the following set of pressure-volume data to develop a script m-file to find a0 and a1 in the following exponential model.
After the model development:
Calculate the root of developed model equation using Newton-Raphson method (xinitial=0). Then,
The code should automatically calculate the predicted volume @ 1.05 bar pressure using the regression model. Also, in the same script m-file this value must be calculated using Lagrange Interpolation and the absolute difference between two finding must be displayed as well.
Data
pressure (bar) volume(m^3)
0.644 32
0.985 25
1.108 22.2
1.363 18
1.631 15
1.934 12
2.356 9
3.1 7.2
Note: The script m-file should work itself . All inputs must be defined in the beginning of the code.
Warnings:
The results should be presented with fprintf command.
The following results should be displayed at the end of the code in this order (just these)
a- the model, with a0 and a1 written on it
b- root of the model equation
c- result of the predicted volume at 1.05bar using regression model
d- interpolation result of volume at 1.05bar using lagrange method,
e- the absolute difference between the volume values calculated from model and interpolation.
When script executed above text with correct result should be displayed on command window: answers:
a) The Model: y=(…………)+(…………)*x+(…………)*x^2
b) root value : ……………
c) From model: V=………… when P=1.05bar
d) According to LAGRANGE Method
f interpolated value for P = 1.05 -> V= …………
e) absolute difference between calculated value and interpolation
difference: …………

回答(2 个)

Debra Bastian
Debra Bastian 2022-1-22
Try instead of a single slash / in text constants, use a double //, because a single one is used to enter system characters, byte values, and you never know.

Image Analyst
Image Analyst 2022-1-22
Instead of using (because fprintf() does not like it):
syms xvalue
create xvalue numerically from the other inputs, or from using linspace().
xvalue = linspace(startingx, endingx, numberOfElements);

Community Treasure Hunt

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

Start Hunting!

Translated by