フィルターのクリア

How do I avoid the mistake?

4 ビュー (過去 30 日間)
Alisa-Oleksandra Kotliarova
Alisa-Oleksandra Kotliarova 2023 年 12 月 11 日
I have the following function:
function [x,y,k] = Hord(fun,a,b,tolx,toly)
ya=feval(fun,a);
k=0;
while 1
k=k+1;
x=(a*fun(b)-b*fun(a))./(fun(b)-fun(a));
y=feval(fun,x);
if(x(k)-x(k-1)<=tolx)&(abs(y)<=toly), break, end
if y*ya>0; a=x; ya=y;
else b=x;
end
end
Then my code is:
clc, clear all, close all, format short
x1=linspace(-pi/2,pi/2);%Заданий інтервал значень;
y1=cos(x1+0.3)-x1.^2;%Функція 1;
figure(1), plot(x1,y1,[-pi/2,pi/2], [0,0]),grid on, hold on%Графік;
f1=(@(x1) cos(x1+0.3)-x1.^2);%Анонімне задання функції 1;
format long
[X1,Y1] = Hord(f1, 0, pi/2, 1e-3, eps);%Перший додатний корінь;
X1,Y1%Виведення кореня;
plot(X1,Y1,'o','r--')%На графіку;
It should output the graph with a root, however, it only gives:
Array indices must be positive integers or logical values.
Error in Hord (line 9)
if(x(k)-x(k-1)<=tolx)&(abs(y)<=toly), break, end
Error in Lab7_var2 (line 36)
[X1,Y1] = Hord(f1, 0, pi/2, 1e-3, eps);%Перший додатний корінь;
How can I correct it?

採用された回答

Torsten
Torsten 2023 年 12 月 11 日
編集済み: Torsten 2023 年 12 月 11 日
x in your code is a scalar, not a vector. Thus something like x(k) does not exist.
You could work with xold and x where xold would be the x-value of the previous iteration.
  1 件のコメント
Alisa-Oleksandra Kotliarova
Alisa-Oleksandra Kotliarova 2023 年 12 月 13 日
Thanks, that helped a lot!

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2023 年 12 月 13 日
  1 件のコメント
Alisa-Oleksandra Kotliarova
Alisa-Oleksandra Kotliarova 2023 年 12 月 13 日
Thank you!

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

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by