필터 지우기
필터 지우기

How to use the polyfit block in Simulink

조회 수: 21 (최근 30일)
Jiazeng Shan
Jiazeng Shan 2011년 5월 26일
I want to do the same thing in simulink as the commands shown below: clear all clc x=[0:0.01:1]'; a=[-2 5 6]'; f=@(x) a(1)*x.^2+a(2)*x+a(3); y=f(x); factor=polyfit(x,y,2); factor =
-2.0000 5.0000 6.0000
But when I use the "simin" that read the [x,y] as the input and polyfit block to assess the coefficients, and I set the Control points(X) with x, there are two error alerts:
1)Error in port widths or dimensions. Output port 1 of 'mean_0525/Least Squares Polynomial Fit/Constant' is a [101x3] matrix.
2)Error reported by S-function 'sdspqreslv2' in 'mean_0525/Least Squares Polynomial Fit/QR Solver':
The number of rows of input A must match the number of rows of input B.
How could I solve this problem. Thanks!

답변 (6개)

Kaustubha Govind
Kaustubha Govind 2011년 5월 26일
How are you feeding in 'y' to the "Least Squares Polynomial Fit" block? The length of the input to the block must be the same as the length of 'x'. Try using a "From Workspace" block to get 'y'.
  댓글 수: 1
Jiazeng Shan
Jiazeng Shan 2011년 5월 26일
Yes, I use the block From Workspace "simin" first to read [x,y] as a input, they have the same length as shown in the codes. I think x as a time variable and y as a input so I set the Control ponits in the polyfit block with x. And there are the alerts...

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


Fangjun Jiang
Fangjun Jiang 2011년 5월 26일
A wild guess. Can you try the Control points as x'? since your x is 101x1.
  댓글 수: 1
Jiazeng Shan
Jiazeng Shan 2011년 5월 26일
I tried but no sense. I also tried to define the input as [x';y'], the number of rows of input B (in error 2) has been changed from 1 to 100. But the rows of inputs are 101. There is one example in matlab named doc_leastsquarespolyfit_ref.mdl. In that situation it works...

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


Jiazeng Shan
Jiazeng Shan 2011년 5월 26일
Actually, my question is simple that can I do the same process in simulink as the codes did shown above?

Arnaud Miege
Arnaud Miege 2011년 5월 26일
Use the Polynomial block.
HTH,
Arnaud
  댓글 수: 5
Arnaud Miege
Arnaud Miege 2011년 5월 26일
If your a and x are changing at each time step, then you probably need to use a MATLAB Function block (http://www.mathworks.com/help/releases/R2011a/toolbox/simulink/slref/matlabfunction.html). I tried it with the following code:
function [y, factor] = fcn(a,x)
%#codegen
y = a(1)*x.^2+a(2)*x+a(3);
factor = polyfit(x,y,2);
and it works well. I fed to the x input a ramp signal with a slope of 1e-3 so that it ramps from 0 to 1 in 10s, and to the a input a constant block with [-2 5 6].
Note that this is in R2011a. In previous releases, the MATLAB Function block was called "Embedded MATLAB Function" block and the code may be slightly different, in particular the %#codegen hasthtag.
HTH,
Arnaud
Jiazeng Shan
Jiazeng Shan 2011년 5월 26일
Thank you Arnaud. I can do it outside of matlab. The "a" will not change during every step. But for the polyfit is only one step of my simulink model, if I can't achieve it in the simulink...I have to break my model into two models and finish the polyfit using the codes in the matlab.

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


Arnaud Miege
Arnaud Miege 2011년 5월 26일
If a doesn't change, then surely factor = a? If that's the case, a simple Polynomial block parameterized with a should do the trick and produce y when fed with x. There's no need for polyfit.
If a does change, use the Embedded MATLAB Function block to include the MATLAB code in the Simulink model, as suggested in my previous comment.

Fangjun Jiang
Fangjun Jiang 2011년 5월 27일
I am kind of lost. I don't have the DSP toolbox. Can you repeat the example shown here in Polyfit? Try this before using the Inport block to get in your input.

카테고리

Help CenterFile Exchange에서 Event Functions에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by