필터 지우기
필터 지우기

How to fix broken function code?

조회 수: 2 (최근 30일)
Jenni
Jenni 2024년 2월 7일
댓글: Jenni 2024년 2월 7일
Hey! Here is my function and I am trying to estimate the values of b_1 and b_0, but when I am running my code, I get following messages:
"Error using/
Matrix dimensions must agree."
"Error in function sovittaja (line 5):
s_2 = sum((x.*y)/(delta_y.^2));"
"Error in (line 16):
sovittaja(f,v,delta_v).
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)
What should I do to make the code work? And how could I plot these?
Thank you for the help!

채택된 답변

Stephen23
Stephen23 2024년 2월 7일
편집: Stephen23 2024년 2월 7일
Use array division here:
s_2 = sum((x.*y)./(delta_y.^2));
% ^^
and here:
s_5 = sum(x.^2./(delta_y.^2));
% ^^
and possibly in some other locations: check all division and power operations!
Learn the difference:
As a rule of thumb: if you are not doing linear algebra then use array operations.
  댓글 수: 1
Jenni
Jenni 2024년 2월 7일
Great thank you, the code is working now!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Biomedical Signal Processing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by