0
votes

I want to fit the second degree equation to the graph like Figure 1 shows.

I have already "From workspace" blocks and "Scope" blocks Figure 2.

Now my problem is how do I fit second degree graph. I have already "constant" block where I have "cars" matrix. Then I have "ramp" block which creates slope between 1-365 like Figure 3 shows.

Then I have "Least Squares Polynomial Fit" and "Polynomial Evaluation" blocks Figure 4. What I am doing wrong because it isn't working?

This is my matlab code:

n = importdata('n.txt',';')
cars = n(:,2)
trucks = n(:,3)
bus = n(:,4)
t = linspace(1,365,365)
t = transpose(t)

It gives me error "Error in port widths or dimensions. Output port 1 of .. is a [32x3] matrix." and "The number of rows of input A must match the number of rows of input B."

1
What does "it isn't working" mean? Please expand.Phil Goddard
It gives me error "Error in port widths or dimensions. Output port 1 of .. is a [32x3] matrix." and "The number of rows of input A must match the number of rows of input B."engineerstudent
Please edit your question to include the error messages, not bury them in the comments.Phil Goddard

1 Answers

0
votes

Given the model you show, I assume you have looked at the example for the Least Square Polynomial Fit block, and completely misunderstood what it is doing.

It is showing an example where the coefficients of a polynomial as a function of time are known, then using the Polynomial Evaluation block to evaluate the polynomial, then using the Least Square Polynomial Fit to get the coefficients of the polynomial back again. That is, it is showing that when data is "round tripped" though both blocks you get back to where you started, supposedly giving you confidence that the blocks are "doing the right thing".

But what they show is not what you are wanting to do, and hence you need a different model.

If you look at the documentation for the Polynomial Evaluation block, there are several examples of what the inputs are expected to be.

The first input is the independent variable, which in your case appears as if it should be simulation time. You can use a Ramp for this, but why not just use the Clock block. (If you do use a Ramp then you want it to have an Initial Value of 0, not 365 as you show in the image in your question.)

The second input are the coefficients of the polynomial as a vector. These could be pregenerated (using for instance MATLAB's polyfit) function, or could be generated on the fly using the Least Square Polynomial Fit block.

That is, it looks like you really want the following model:

Polyval