How can i solve function with an dependent variable?
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천

There is a g(u) function and u variable's values are dependent to g(u) function, its derivative and smaller u.
I do not know how to write this as a matlab code.
채택된 답변
Rik
2021년 6월 10일
Below I show the structure of a possible solution. The rest is up to you. Feel free to comment if you have a specific question.
g=@(mu,B1,B2,B3,a1,a2,a3) B1./a1;
g_=@(mu,B1,B2,B3,a1,a2,a3) 2./(a1-mu);
mu_k=0;
while true
mu_kp1=mu_k-g(mu_k,B1,B2,B3,a1,a2,a3)./g_(mu_k,B1,B2,B3,a1,a2,a3);
if mu_kp1/mu_k < 1
break
end
end
댓글 수: 6
i tried but it was not working. how can i write u as matrix? so i thought i could create a for loop by giving k value.
You can encode mu as a vector if you want. Is that interesting?
And what did you try? The code I showed isn't finished, I left the specifics for you. Please show you put in some effort.
Below is the edit required to make mu a vector.
g=@(mu,B1,B2,B3,a1,a2,a3) B1./a1;
g_=@(mu,B1,B2,B3,a1,a2,a3) 2./(a1-mu);
mu=0;k=1;
while true
mu(k+1)=mu(k)-g(mu(k),B1,B2,B3,a1,a2,a3)./g_(mu(k),B1,B2,B3,a1,a2,a3);
if mu_(k+1)/mu(k) < 1
break
end
end
g = @(u) (B1./(al1 - u)).^2 + (B2./(al2 - u)).^2 + (B3./(al3 - u)).^2 -1;
derivg = @(u) 2/(al1-u).*(B1./(al1 - u)).^2 + 2/(al2-u).*(B2./(al2 - u)).^2 + 2/(al3-u).*(B3./(al3 - u)).^2 ;
u=0; k=1;
while true
u(k+1)= u(k)-g(u(k))./derivg(u(k));
if abs(u(k)- u(k-1)./u(k)) < 0.000001
break
end
end
i did this but i got this error "Unable to perform assignment because the left and right sides have a different number of elements." at
u(k+1)= u(k)-g(u(k))./derivg(u(k));
line.
and said that i should consider preallocating "u". (btw other variables are constant.)
I do not know how to preallocate it.
u = zeros(1, 1000000);
i preallocated like this. However i still have the error "Unable to perform assignment because the left and right sides have a different number of elements.".
After a minor edit, the code runs fine. And you only need pre-allocation if you expect many iterations, in which case I would suggest avoiding storing all intermediate values, unless the explicit goal is to find the trend in iterations of mu for different values of your betas and alphas.
%generate random values, but make sure they are the same every run
rng(0);[B1,al1,B2,al2,B3,al3]=deal(rand);
g = @(u) (B1./(al1 - u)).^2 + (B2./(al2 - u)).^2 + (B3./(al3 - u)).^2 -1;
derivg = @(u) 2/(al1-u).*(B1./(al1 - u)).^2 + 2/(al2-u).*(B2./(al2 - u)).^2 + 2/(al3-u).*(B3./(al3 - u)).^2 ;
u=0; k=1;
while true
u(k+1)= u(k)-g(u(k))./derivg(u(k));
k=k+1;
if abs((u(k)- u(k-1))./u(k)) < 0.000001
% ^ ^
% you forgot these
break
end
end
disp(u)
0 -0.2716 -0.4929 -0.5853 -0.5963 -0.5964 -0.5964
thank you so much for helping me
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Programming에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
