Linear Regression problem issues

7 次查看(过去 30 天)
hello to everyone can anyone help me to answer to this question?it's hard for me please help me.
  7 个评论
parsa soleimani
parsa soleimani 2021-2-21
this is my fault itried hard but i coudn't answer it
Adam Danz
Adam Danz 2021-2-21
编辑:Adam Danz 2021-2-22
So your assignment is to
  1. plot the data
  2. compute the linear fit, preferably using the mldivide operator
  3. plot the fitted line
  4. Put that all into a funtion.
It looks like you're starting with step 4 but the other steps are more important.
Try step 1 first. You already have the x and y values. You just have to plot them (doc plot).
Learn about step 2 in the link I provided above (mldivide).

请先登录,再进行评论。

回答(4 个)

Image Analyst
Image Analyst 2021-2-21
编辑:Image Analyst 2021-2-21
Can you simply use polyfit(x, y, 1)? Or do you have to use the slash operator? And obviously you need more than 1 point if you're going to fit a line
numPoints = 300; % Whatever....
x = sort(-5 + 10*rand(1, numPoints), 'ascend');
y = x + rand(1, numPoints);
  4 个评论
parsa soleimani
parsa soleimani 2021-2-22
i must use function in this issue not others and i don't know how can i fix it in this page i sent the image of my problem

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2021-2-22
function=lin_reg
function is a keyword that cannot be assigned a value. The syntax you need is the word function followed spaces followed by a list of output variable names in [], followed the name of the function, followed by the list of input parameters names in ()
row vectors=2
In matlab variable names cannot have spaces in them. That line turns out to be valid matlab syntax, but it would mean that a function named row is to be called with a single parameter that was the character vector 'vectors=2'. There does not happen to be an existing matlab function named row but it would be valid to define one.
input arguments(containing x and y coordinates of points)
In matlab that would mean to call the function named input with a single parameter that is a character vector containing 'arguments(containing x and y coordinates of points)'
It happens that there is an existing matlab function named input. It would display those characters as a prompt and then wait for the user to type a line of input. It would then execute the input as an expression and prepare to return the value. The value would get displayed and assigned to the special variable named ans
function=lin_reg
Error: Function definition not supported in this context. Create functions in code file.
You cannot define functions at the command line. Functions must be stored in a file whose name is a valid variable name, followed the extension .m . In this case you would need to put the code into lin_reg.m
scalars=2
That is a valid line. It would assign the double precision number 2.0 to the variable named scalars. Then the default action is to display the name of the variable and the new value of the variable.
scalars =
2
and there you see the name of the variable and its value being displayed.
[a b] = lin_reg(x,y)
Unrecognized function or variable 'x'.
You do want that text, but in this case you need the word function first on the line
function [a b] = lin_reg(x,y)
would be a fine line to put at the beginning of file lin_reg.m
However you did not have the word function first so matlab tried to execute the line, and ran into the problem that the variable x is not defined.
x=1
x =
1
y=0
y =
0
Those are valid assignments of values to x and y and the default display of new content
plot
Error using plot
Not enough input arguments.
There are some functions that can be called without parameters but plot function always needs at least one parameter indicating which data is to be plotted.
end
end
Error: Illegal use of reserved keyword "end".
If you were in a function definition it would be valid to have an end statement at this point. But you are at the command line and not defining a function so it is an error here
end
end
Error: Illegal use of reserved keyword "end".
Error for same reason.
  2 个评论
Walter Roberson
Walter Roberson 2021-2-22
putting these together: you could create file lin_reg.m that had
function [a b] = lin_reg(x,y)
x=1
y=0
plot
end
That would be valid syntax. It would fail because plot needs to be passed at least one parameter but the syntax would be valid.
Walter Roberson
Walter Roberson 2021-2-22
Now, if you look closely at what you are asked to do, your function should not do any plotting itself. They have provided you with an additional script or function to execute. That script or function creates some data and passes it to your function. Your function is responsible for analyzing the data and returning two output variables without displaying anything to the command line and without plotting. Then the function or script they provide will create a plot on your behalf.
Your code is not responsible for creating the input values or displaying the plot. It is only responsible for accepting two input variables and calculating two output variables.

请先登录,再进行评论。


parsa soleimani
parsa soleimani 2021-2-22
this is the full issue that i must type code in function part.what is your idea?

Image Analyst
Image Analyst 2022-6-4
See full demo attached.

类别

Help CenterFile Exchange 中查找有关 Calendar 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by