matlab Fit the third polynominal

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 comentarios

dpb
dpb el 4 de Dic. de 2020
See
doc polyfit
and friends...
Nora Tarek
Nora Tarek el 4 de Dic. de 2020
i donot want to use polyfit
Matt J
Matt J el 4 de Dic. de 2020
OK, but what is your question? Is something not working? One problem I see immediately is this expression,
elseif 10.042<H<18.3935
It should really be,
elseif 10.042<H & H<18.3935
Nora Tarek
Nora Tarek el 4 de Dic. de 2020
Editada: Nora Tarek el 4 de Dic. de 2020
i cannot make it take the user input data H and find the four points that the code will use to fit the third degree ploynominal like if the use entered 14 which is a number between the points in y
then the four points that we should use is(50,12.3015) and (62.5, 13.1827) and (75, 14.5476)and ( 87.5 ,16.0764) to fit the third degree polynominal
However if it chose outside the range of Y there will be a message that what he entered is invalid
In addition to what Matt J noted you have another similar problem.
if H>18.3935 && H<10.0427
Can you give me an example of a number that is simultaneously greater than 18 and less than 10?
dpb
dpb el 4 de Dic. de 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 el 4 de Dic. de 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."

Iniciar sesión para comentar.

 Respuesta aceptada

Matt J
Matt J el 4 de Dic. de 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 comentario

dpb
dpb el 4 de Dic. de 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.

Iniciar sesión para comentar.

Más respuestas (1)

dpb
dpb el 4 de Dic. de 2020
Editada: dpb el 4 de Dic. de 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 comentario

dpb
dpb el 4 de Dic. de 2020
Editada: dpb el 4 de Dic. de 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);
>>

Iniciar sesión para comentar.

Categorías

Preguntada:

el 4 de Dic. de 2020

Editada:

dpb
el 4 de Dic. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by