matlab Fit the third polynominal

1 view (last 30 days)
Nora Tarek
Nora Tarek on 4 Dec 2020
Edited: dpb on 4 Dec 2020
this is my code
n=3;
A=zeros(n+1,n+1);
B= zeros (n+1,1);
a=zeros (n+1,1);
for row=1 :n+1
for col=1 :n+1
if row ==1 &&col==1
A(row,col)== m;
continue
end
A(row,col)= sum (x.^(row+col-2));
end
B(row)= sum(x.^(row-1).*y);
end
a =A\B
end
end
from here I want the user to input a number thisnumber musnot be less than the values exsist in Y and when this user enter the number m the four points that my program will do to fit the third degree polynomial the user entered number must be between them ( so two points before my number and two points after )
  7 Comments
dpb
dpb on 4 Dec 2020
" donot want to use polyfit"
Why in the world not? It's much simpler and uses the same internals as does backslash.
"my program will do to fit the third degree polynomial the user entered number must be between them ( so two points before my number and two points after ) "
What if the user enters 5.3, say?
HINT:
doc interp1 % Use backwards of normal for inverse lookup with 'next' or 'previous' method
Steven Lord
Steven Lord on 4 Dec 2020
Why in the world not? It's much simpler and uses the same internals as does backslash.
I'm 95% sure the answer is "because it's a homework assignment."

Sign in to comment.

Accepted Answer

Matt J
Matt J on 4 Dec 2020
This might be what you want:
x=[0,12.5,25,37.5,50,62.5,75,87.5,100];
y=[10.0427, 10.3969,10.9316,11.5673,12.3015,13.1827,14.5476,16.0764,18.3935];
ymin=min(y);
ymax=max(y);
Method=input('Input the physical propety you want , Ultimate strenght (1), Toughness (2), Young’s modulus (3): ');
if Method==1
disp('Enter parameters');
H=input('enter the number you want ');
if ~(H>ymin & H<ymax)
disp ( "Enter a value between " +ymin+ " and " +ymax)
else
a=polyfit(x,y,3);
end
end
  1 Comment
dpb
dpb on 4 Dec 2020
That doesn't select the set of points bracketing the user input for the specific fit the OP is looking for.
Probably a better and certainly easier to code. I'd suggest would be to use interp1 with 'pchip' method.

Sign in to comment.

More Answers (1)

dpb
dpb on 4 Dec 2020
Edited: dpb on 4 Dec 2020
I'm guessing something on the lines of the following would be about the ticket...
nProp=listdlg('ListString',{'Ultimate strength', 'Toughness', 'Young''s modulus'}, ...
'Name','MATERIAL PROPERTIES','PromptString', ...
'Select Desired Properties', ...
'SelectionMode','single', ...
'ListSize',[150 150]);
switch nProp
case 1
x=[0,12.5,25,37.5,50,62.5,75,87.5,100];
y=[10.0427, 10.3969,10.9316,11.5673,12.3015,13.1827,14.5476,16.0764,18.3935];
xmin=min(x); xmax=max(x);
ymin=min(y); ymax=max(y);
H=input("Enter the Independent Variable Value between " +xmin+ " and " +xmax+ ": ");
if ~iswithin(H,xmin,xmax)
error("Enter a value between " +xmin+ " and " +xmax)
end
PropVal=interp1(x,y,H,'pchip');
case 2
....
end
I didn't handle the error; just abort and also made the presumption that it is really x and not y that is the independent variable -- using descriptive variable names would go a long way towards helping be able understand the code intent.
One could use a slider or other input technique to bound the input to be much less user beligerent than just input
  1 Comment
dpb
dpb on 4 Dec 2020
Edited: dpb on 4 Dec 2020
Oh. iswithin is my handy-dandy utility routine
>> type iswithin
function flg=iswithin(x,lo,hi)
% returns T for values within range of input
% SYNTAX:
% [log] = iswithin(x,lo,hi)
% returns T for x between lo and hi values, inclusive
flg= (x>=lo) & (x<=hi);
>>

Sign in to comment.

Categories

Find more on Polynomials in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!