# How do I make a function work?

조회 수: 1 (최근 30일)
Alisa-Oleksandra Kotliarova 2023년 11월 17일
댓글: Voss 2023년 11월 21일
function Kram=linsolve (A,B)
[m,n]=size(A);
x=zeros(m,1);
for i=1:m
xd = det(Ad) / det(A);%Корінь рівняння;
x(i,1)=xd;%Вектор коренів;
end
end
This function solves the problem of roots in this way:
A=randn(2), B=rand(2,1), Kram=linsolve (A,B)
A =
-0.9606 0.7612
-1.6338 1.1933
B =
0.9345
0.1079
Kram =
10.6159
14.6250
However, it doesn´t do the solution checking right:
x_r=x
x_r =
Columns 1 through 10
-4.5000 -4.2374 -3.9747 -3.7121 -3.4495 -3.1869 -2.9242 -2.6616 -2.3990 -2.1364
Columns 11 through 20
-1.8737 -1.6111 -1.3485 -1.0859 -0.8232 -0.5606 -0.2980 -0.0354 0.2273 0.4899
Columns 21 through 30
0.7525 1.0152 1.2778 1.5404 1.8030 2.0657 2.3283 2.5909 2.8535 3.1162
Columns 31 through 40
3.3788 3.6414 3.9040 4.1667 4.4293 4.6919 4.9545 5.2172 5.4798 5.7424
Columns 41 through 50
6.0051 6.2677 6.5303 6.7929 7.0556 7.3182 7.5808 7.8434 8.1061 8.3687
Columns 51 through 60
8.6313 8.8939 9.1566 9.4192 9.6818 9.9444 10.2071 10.4697 10.7323 10.9949
Columns 61 through 70
11.2576 11.5202 11.7828 12.0455 12.3081 12.5707 12.8333 13.0960 13.3586 13.6212
Columns 71 through 80
13.8838 14.1465 14.4091 14.6717 14.9343 15.1970 15.4596 15.7222 15.9848 16.2475
Columns 81 through 90
16.5101 16.7727 17.0354 17.2980 17.5606 17.8232 18.0859 18.3485 18.6111 18.8737
Columns 91 through 100
19.1364 19.3990 19.6616 19.9242 20.1869 20.4495 20.7121 20.9747 21.2374 21.5000
>> F=A*x_r-B
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix
matches the number of rows in the second matrix. To perform elementwise multiplication, use '.*'.
Related documentation
What should I do?

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

### 채택된 답변

Voss 2023년 11월 17일
Your function returns a variable called Kram, but no variable with that name is calculated within the function.
I assume you want to return what you call x in the function.
format long g
A=randn(2), B=rand(2,1)
A = 2×2
-0.0823460205646498 -2.21863794181447 1.6232134952347 1.04101266770237
B = 2×1
0.120008128513305 0.0050788186937395
x_r = lin_solve(A,B)
x_r = 2×1
0.0387410540739482 -0.0555287989206656
F=A*x_r-B
F = 2×1
1.0e+00 * 1.38777878078145e-17 6.93889390390723e-18
function x=lin_solve(A,B)
[m,n]=size(A);
x=zeros(m,1);
for i=1:m
xd = det(Ad) / det(A);%Корінь рівняння;
x(i,1)=xd;%Вектор коренів;
end
end
##### 댓글 수: 2없음 표시없음 숨기기
Alisa-Oleksandra Kotliarova 2023년 11월 21일
Thank you so much, that helped a lot!
Voss 2023년 11월 21일
You're welcome!

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

### 추가 답변 (1개)

Sulaymon Eshkabilov 2023년 11월 17일
There are a couple of critical points:
(1) Don't use MATLAB's builtin fucntion names for your own functions that corrupts MATLAB fcns, i.e.:
% function Kram= lin_solve(A,B); % instead of linsolve (A,B)
% [m,n]=size(A);
% x=zeros(m,1);
% ...
% end
(2) It is not clear what you are try to do here inside the above fcn:
% for i=1:m
% xd = det(Ad) / det(A);%Корінь рівняння;
% x(i,1)=xd;%Вектор коренів;
% end
(3) If you want to verify the found solution and compute the error. It can be done in this way, i.e.:
A=randn(2); B=rand(2,1); X_Sol=linsolve(A,B);
Error = norm(A*X_Sol-B)/norm(B)
Error = 0
(4) If you have many sets of equations and want to verify their solutions, it can be simulated something like this way:
for ii = 1:10
rng(ii)
A=randn(2); B=rand(2,1);
X_Sol(:,ii)=linsolve(A,B); % store solutions if necessary
Error = norm(A*X_Sol(:,ii)-B)/norm(B)
end
Error = 7.0704e-17
Error = 1.0383e-16
Error = 8.7752e-17
Error = 3.7999e-17
Error = 3.1714e-16
Error = 0
Error = 1.9889e-16
Error = 0
Error = 0
Error = 1.0151e-16
##### 댓글 수: 1이전 댓글 -1개 표시이전 댓글 -1개 숨기기
Alisa-Oleksandra Kotliarova 2023년 11월 21일
Thanks a lot, that`s quite useful!

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

### 카테고리

Help CenterFile Exchange에서 Correlation and Convolution에 대해 자세히 알아보기

### Community Treasure Hunt

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

Start Hunting!

Translated by