필터 지우기
필터 지우기

Polyfit works in command window, but returns incorrect values in M-File

조회 수: 2 (최근 30일)
Zac
Zac 2011년 7월 21일
I can't go into too much detail on the application of my code because it is for a confidential project. What I am working on though interfaces with Prolith, a microlithography simulation tool. So posting the entirety of my code here would be useless. My process is as follows:
  1. Run a bunch of simulations in Prolith from MATLAB
  2. Get data from Prolith from simulations
  3. Analyze data to determine response
  4. Use polyfit function to model response quadratically
My function returns four values:
  • dCDA: One of the responses
  • sCDA: The second response
  • dCDA_model: the modeled dCDA
  • sCDA_model: the modeled sCDA
Now here is my problem, when I run my function it returns the incorrect values. However, running the same code from the command window returns the correct values. My x values are in a vector called ZMag.
EDIT: See comment below for corrected data
ZMag =
0 1.93 3.86 5.79 7.72 9.65
dCDA =
-0.0811 0.1554 0.4109 0.7142 1.0336 1.3741
0.0635 0.4425 0.8215 1.2006 1.5791 1.9525
-0.0099 -0.0079 -0.0078 -0.0090 -0.0117 -0.0159
0.0526 -0.1442 -0.3442 -0.5637 -0.7802 -0.9938
sCDA =
-0.1050 -0.5570 -1.0011 -1.4324 -1.8442 -2.2308
-0.1034 0.2494 0.6041 0.9594 1.3145 1.6516
-0.0187 0.1713 0.3622 0.5530 0.7437 0.9392
-0.0908 -0.2982 -0.4927 -0.6840 -0.8622 -1.0330
dCDA_model (incorrect)
151.5506 -15.3017 -19.9054 5.5466
21.1869 38.5986 0.9280 -21.4042
-0.0715 0.0594 -0.0144 0.0662
sCDA_model (incorrect)
155.0786 -33.6812 -19.3941 4.9035
-50.9605 36.9912 20.3325 -18.6355
-0.0700 -0.1135 -0.0295 -0.1105
dCDA_model (correct)
0.0041 -0.0004 -0.0005 0.0001
0.1098 0.2000 0.0048 -0.1109
-0.0715 0.0594 -0.0144 0.0662
sCDA_model (correct)
0.0042 -0.0009 -0.0005 0.0001
-0.2640 0.1917 0.1053 -0.0966
-0.0700 -0.1135 -0.0295 -0.1105
And finally, the code that generated these anomalies:
for i = 1:n
dCDA_model(:,i) = polyfit(ZMag,dCDA(i,:),2);
sCDA_model(:,i) = polyfit(ZMag,sCDA(i,:),2);
end
Any ideas as to what could be causing this disparity in results between the command window and M-File? I am using MATLAB R2007a.
Thank you.
  댓글 수: 1
Zac
Zac 2011년 7월 21일
I truncated my matrices because they are rather long, I didn't even think it would change the modeled curve. Anyway, lets just work with one complete vector.
ZMag = 0 1.93 3.86 5.79 7.72 9.65 11.58 13.51 15.44 17.37 19.3
dCDA = -0.0811 0.1554 0.4109 0.7142 1.0336 1.3741 1.7407 2.1389 2.5751 3.0548 3.5848
sCDA = -0.1050 -0.5570 -1.0011 -1.4324 -1.8442 -2.2308 -2.5865 -2.9047 -3.1793 -3.4040 -3.5827

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

답변 (3개)

Sean de Wolski
Sean de Wolski 2011년 7월 21일
EDIT
Both look good to me...
dCDA_model = polyfit(ZMag,dCDA,2); %your new data
sCDA_model = polyfit(ZMag,sCDA,2);
subplot(121)
plot(ZMag,dCDA,'b-',ZMag,polyval(dCDA_model,ZMag),'rp')
subplot(122)
plot(ZMag,sCDA,'b-',ZMag,polyval(sCDA_model,ZMag),'rp')
And they both work in mfile/desktop. That's why I really am wondering if you're calling ML's polyfit
which polyfit
?
  댓글 수: 2
Zac
Zac 2011년 7월 21일
I truncated my matrices because they are rather long, I didn't even think it would change the modeled curve. Anyway, lets just work with one complete vector.
ZMag = 0 1.93 3.86 5.79 7.72 9.65 11.58 13.51 15.44 17.37 19.3
dCDA = -0.0811 0.1554 0.4109 0.7142 1.0336 1.3741 1.7407 2.1389 2.5751 3.0548 3.5848
sCDA = -0.1050 -0.5570 -1.0011 -1.4324 -1.8442 -2.2308 -2.5865 -2.9047 -3.1793 -3.4040 -3.5827
Regardless of getting the same results, doesn't it seem strange that it's fitting such a large parabola (in the incorrect case) to such small numbers?
Zac
Zac 2011년 7월 21일
Also, prior to calling the polyfit function there is the end of the for loop in which *CDA are calculated. That is all that is prior to the call.

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


Zac
Zac 2011년 7월 21일
Update: Tried putting code into its own function in a separate m-file and it returns the same value. How could I be calling MATLAB's polyfit from the desktop, but some other polyfit within the function? Doing a search for "poly" through my code finds only the spot in question. Could you guide me in finding out if I am really not calling MATLAB's function?

Zac
Zac 2011년 7월 22일
It is calling MATLAB's polyfit,
which polyfit
C:\Program Files\MATLAB\R2007a\toolbox\matlab\polyfun\polyfit.m
I just made a callback script to call polyfit outside of the function. It's clunky, but it works. I'm not sure why though.

카테고리

Help CenterFile Exchange에서 Switches and Breakers에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by