plot a function of 2 variables inside a for loop
조회 수: 13 (최근 30일)
이전 댓글 표시
I have this equation where A,B are onstants and E= ( 1-( ((1-B)/2.*J).*(1+(E.*J/(A*B)))*sqrt(E.*J.*A/B) ));
I need to plot E as J varies from 0:300 however when I evaluate E inside a for loop from 0:300 using solve command I get error in using indices. This is the piece of code:
A=5;
B=0.9;
J=0:300;
syms E J
for i= 1:lenght(j)
E(i)= solve(( 1-( ((1-B)/2.*J(i)).*(1+(E(i).*J(i)/(A*B)))*sqrt(E(i).*J(i).*A/B) )),J(i));
end
댓글 수: 0
채택된 답변
Rik
2021년 12월 7일
You are overwriting J and using j in your loop definition.
You should define an array to hold the result. Then you can define E as a symbolic variable. Since J has a definite value, it is not symbolic.
Also, don't use i or j as variable names and use numel or size instead of length.
The last change you need to make is to not index E. You could also use J directly in the for loop definition, but that is a matter of taste.
댓글 수: 3
Rik
2021년 12월 7일
A= 0.9;
B= 5;
J= 1:300;
E_sol=zeros(size(J));
syms E
w=warning('off','symbolic:solve:PossiblySpuriousSolutions');
for k=1:numel(J)
%You should solve for E, since you want to know the value of E
tmp=solve(( 1-( ((1-B)/2.*J(k)).*(1+(E.*J(k)/(A*B)))*sqrt(E.*J(k).*A/B) )),E);
if k==1,tmp,end %debug information
tmp=double(tmp);
if k==1,tmp,end %debug information
%You probably want to select the real solution
[imag_val,ind]=min(abs(imag(tmp)));
if imag_val > 2*eps
warning('J=%.0f did not return a real solution',J(k))
E_sol(k)=NaN;
continue
end
E_sol(k)=tmp(ind);
end
warning(w);%reset warning state
E_sol
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Calculus에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
