Hello,
I am having difficulties in writing a Raphson method using a while loop. Matlab keeps returning error: y(0): "subscripts must be either integers 1 to (2^63)-1 or logicals". I have tried to change my code several times, to try to solve this problem, but I have been unsuccesful. I am wondering if anyone has any suggestions on how to solve this problem?

댓글 수: 4

Star Strider
Star Strider 2023년 9월 17일
The problem is that it appears that you are using C syntax in MATLAB. That will not work. You probably need to re-write the entire file in valid MATLAB code.
That aside, in MATLAB, indexing begins at 1, not 0, and subscripts (indices) need to be integers greater than 0.
Alexander
Alexander 2023년 9월 17일
Thanks for the tips! It did help a lot, I was not able to identify the exact problem. It seems that my function y is not defined for any nonintegers and x = 0. Since x only numbers in the span [0, 1], it will not be defined for any value of x except 1, since y is neither defined for y(0). Any ideas why this can be? The strangest thing is that the plot function works. But if you try for example y(0.02) it will not work.
L = 1;
x = linspace(0,1,100);
y = (8*x)/(3*L) - 3*(x/L).^2 + (1/3)*(x/L).^3 - (2/3)*sin((pi*x)/L);
uy = 1/6*y;
plot(x,y)
Dyuman Joshi
Dyuman Joshi 2023년 9월 17일
You have to loop over the values [1 2 3 ... length(x)], right? If yes, why not use a for loop? Because you are not breaking out of the while loop if a condition is met.
Also, I don't understand why you are adding x(i) to i, in the last line of the while loop.
Alexander
Alexander 2023년 9월 17일
Switched to a for loop and it works much better

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

 채택된 답변

Star Strider
Star Strider 2023년 9월 17일

0 개 추천

My pleasure!
The ‘y’ assignment needs to be an anonymous function, if I understand correctly what you want to do.
That change produces this result —
L = 1;
x = linspace(0,1,100);
y = @(x) (8*x)/(3*L) - 3*(x/L).^2 + (1/3)*(x/L).^3 - (2/3)*sin((pi*x)/L); % Anonymous Function
uy = 1/6*y(x);
figure
plot(x, y(x), 'DisplayName','y(x)')
hold on
plot(x, uy, 'DisplayName','uy(x)')
hold off
grid
xlabel('x')
ylabel('Function Values')
legend('Location','best')
See the documentation on Anonymous Functions for details. Call them as you would any other function. Similar to variables, they must be defined before they are used in the code they are implemented in.
.

댓글 수: 2

Alexander
Alexander 2023년 9월 17일
Thank you so much for your solution!
I got the whole code running now, I switched from a while loop to a much simpler for loop and it runs smoothly!
Star Strider
Star Strider 2023년 9월 17일
As always, my pleasure!
Great!

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

릴리스

R2023b

질문:

2023년 9월 17일

댓글:

2023년 9월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by