How to plot a linear function?

13 ビュー (過去 30 日間)
Jenni
Jenni 2024 年 2 月 7 日
コメント済み: William Rose 2024 年 2 月 9 日
Hey!
Here is my function and I am trying to plot a linear function in form:
y = b_0 + b_1x.
How could I plot this function using the result values b_0 and b_1?
Here is my function code called "sovittaja":
function [b, bci] = sovittaja(x,y,delta_y)
%%Helper variables:
s_1 = sum(1./(delta_y.^2));
s_2 = sum((x.*y)./(delta_y.^2));
s_3 = sum(x./(delta_y.^2));
s_4 = sum(y./(delta_y.^2));
s_5 = sum(x.^2./(delta_y.^2));
%%Defining D:
D = (s_1*s_5)-(s_3)^2;
%%Analyyttiset lausekkeet:
b_1 = (1/D)*((s_1)*(s_2)-(s_3)*(s_4));
b_0 = (1/D)*((s_5)*(s_4)-(s_3)*(s_2));
%%Margin of errors:
delta_b_1 = sqrt((1/D)*(s_1));
delta_b_0 = sqrt((1/D)*(s_5));
%%Indexs of matrix bci :
bci_11 = b_0 - delta_b_0;
bci_12 = b_1 - delta_b_1;
bci_21 = b_0 + delta_b_0;
bci_22 = b_1 + delta_b_1;
%% Return values:
b = [b_0, b_1];
bci = [bci_11,bci_12;bci_21,bci_22];
And here is what I want to do with the function "sovittaja":
clear all
close all
% Measurement:
i=[1,2,3,4,5]';
% Frequency:
x=[519,549,688,740,821]';
% Voltage:
y=[1.0,1.2,1.9,2.4,2.3]';
% Margin of Error:
delta_y=[0.15,0.40,0.20,0.30,0.10];
sovittaja(x,y,delta_y)
So, how could I plot my result to a linear function y = b_0 + b_1x?
And additional question: How could I do the same thing by using command fitlm?
Thank you for the help!
  2 件のコメント
John D'Errico
John D'Errico 2024 年 2 月 7 日
編集済み: John D'Errico 2024 年 2 月 7 日
Why would you want to use fitlm to plot a line? It is not a tool that creates plots. So what are you asking? I think you want to know a tool that can fit a line through your points, using some given weights at those points. I am not entirely sure if the "margin of error" is a set of weights, or it is a set of hard constraints that the line is not allow to pass.
Plotting a line is easy enough, even trivial. Choose two points on the line. Plot them, connecting them with a line.
William Rose
William Rose 2024 年 2 月 7 日
The fitlm help at https://www.mathworks.com/help/stats/fitlm.html is very nice. Read it, try the examples, and you will be good.

サインインしてコメントする。

採用された回答

William Rose
William Rose 2024 年 2 月 7 日
編集済み: William Rose 2024 年 2 月 8 日
[edit: Fix typo: change "values at" to "values are"]
It looks like you want to plot measured data as well as the best-fit straight line.
The measured values are x and y. The predicted values are x and b(0)+b(1)*x.
Please see the instructions and examples here https://www.mathworks.com/help/matlab/ref/plot.html
plot(x,y,'r*',x,b(0)+b(1)*x,'--r');
It also seems like you might want error bars on your plot. If so, see the description and the examples here:
  2 件のコメント
Jenni
Jenni 2024 年 2 月 8 日
Excellent, thank you for the helpful advice and the links!
William Rose
William Rose 2024 年 2 月 9 日
@Jenni, you're welcome.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLinear and Nonlinear Regression についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by