Problem Help!
2 views (last 30 days)
Show older comments
Here are my codes, I keep getting wrong feedback which drive me crazy! Here's the problem: Basicly the problem is to solve a nonlinear equation by using the fzero command. The function is Φ(r)=[Φ0*e^-r/delta]/(4*pi*D*r) And solve r when Φ(r)/Φ0=10% Every parameters are in the command
My problem is: Index exceeds matrix dimensions.
Error in YZHW61 (line 47) fprintf(' %6.3f %2u %6.2f %+6.2E \n',...
%%YZHW61
%Find the distance away from the point source of laser
%
%Written 2/11/2012 by YZ
%Last updated 2/11/2012 by YZ
%
%First list parameters
%μs’=μs(1-g)=10cm-1
%μt’=μa+μs’=10.1cm-1
%D=(1/3)*μt’=3.37cm-1
%δ=sqrt(D/μa)=5.8
%So the final equation becomes Φ(r)=(Φ0*e^(-r/5.8))/(4*pi*3.37*r)
%Simplify the equation and we get:
%Φ=(e^(-r/5.8))/13.48*pi*r where Φ(Phi)=Φ(r)/Φ0 that represents the
%percentage of the new light intensity compared to the original
%light intensity
date
clear all
%First plot Diffusion Equation for r between 0.5 and 100
for i=1:1:200; %loop counters musht be be nonzero,positive integers
r=i/2; %this will give us 0.5cm distance
press(i)=r;
g=0.9;
mius=100;
miua=0.1;
mius1=mius*(1-g);
miut1=miua+mius1;
D=(1/3)*miut1;
delta=sqrt(D/miua);
P(i)=exp(-r/delta)/(4*pi*D*r)*100;
end
hold on
plot(press,P);axis equal; grid
%
% Get user input for the light fluence rate(percentage)
%We need to pass this information to the function by using the global
%variable
global Phi
Phi=0.1;
%
%Choose initial guess
distance=1;
[r,error,exitflag,output]=fzero(@Diffusion,distance);
%
%What's the solution, how many iterations, what's the error?
fprintf=('distance (Iterations fluence rate error) \n');
fprintf(' %6.3f %2u %6.2f %+6.2E \n',...
r, output.funcCount, Phi, error)
plot(r,0.1,'o')
And here is my function:
function Pest=Diffusion(distance)
%Diffusion equation for distance r
%
%First written by YZ on 2/11/2012
%Last updated by YZ on 2/11/2012
%
global Phi
g=0.9;
mius=100;
miua=0.1;
mius1=mius*(1-g);
miut1=miua+mius1;
D=(1/3)*miut1;
delta=sqrt(D/miua);
Pest=exp(-distance/delta)/(4*pi*D*distance)*100-Phi;
end
Really appreicate anyone who can help me!
2 Comments
Walter Roberson
on 12 Feb 2012
What problem are you encountering? There is nothing in your source that an on-looker would clearly understand to be "feedback", and you have not mentioned any error message.
Accepted Answer
Image Analyst
on 12 Feb 2012
Several errors in your code. The big one is this line:
fprintf=('distance (Iterations fluence rate error) \n');
You are not allowed to have an equal sign after fprintf(). I think what you did was to essentially overwrite fprintf with a character string. Then in the next line you tried to access some element of the string beyond the end of it.
Secondly, you are overwriting error(). DON'T DO THAT! It's a built in function. Use "errorStructure" as the name instead of "error".
[r,errorStructure,exitflag,output]=fzero(@Diffusion,distance);
9 Comments
Image Analyst
on 12 Feb 2012
I guess you also eventually remembered to change it in the fprintf() also.
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!