Why wont my code work with fplot?
6 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Sophia Rissberger
il 15 Mag 2019
Commentato: Star Strider
il 16 Mag 2019
I have everything figured out except I cant seem to get the fplot for my anonymous variable A to project over figure(2). This issue may be complicated by the fact that I have a subplot going on in figure(1). The entire code is below, but the very bottom is where Im struggling. I dont get an error messgae, it just simply doesnt plot the function.
clear
clc
clf
%Origonal mass of decaying Radium-226
Anot= 20; %(mg)
%Decay rate
r=-0.000436;%(per year)
%Time elapsed, 20 evely spaced values
t=linspace(1,4000,20); %(years)
%Equation for mass as a function of time
original_data_A=Anot.*(exp(r.*t)); %(mg)
%Creating array the same size as origonal_data_A
numberOfSamples=length(original_data_A);
%Adds noise to original_data_A (+ or - 0.5, then scales each value to 10%)
addSampleNoise=(rand([1 numberOfSamples])-0.5).*(original_data_A.*0.1);
A_noisy=original_data_A+addSampleNoise;
fprintf('First value of A + noise: %0.04f mg \n',A_noisy(1));
fprintf('Last value of A + noise: %0.04f mg \n', A_noisy(end));
figure(1)
%plot linear x-axis vs linear y-axis
subplot(2,2,1)
plot(t,A_noisy,'*')
title('Linear-Linear Fit')
xlabel('Time (years)')
ylabel('Mass (mg)')
axis([0 4000 0 25])
%plot ln(x) vs ln(y)
subplot(2,2,2)
%transform the data from x -> ln(x) and y -> ln(y)
plot(log(t),log(A_noisy),'*')
title('Log-Log Fit')
xlabel('ln(Time (years))')
ylabel('ln(Mass (mg))')
axis([4 10 1 3.5])
%x linear, y-> ln(y)
subplot(2,2,3)
plot(t,log(A_noisy),'*')
title('Linear-Log Fit')
xlabel('Time (years)')
ylabel('ln(Mass (mg))')
axis([0 4000 1 3.5])
%x->ln(x), y linear
subplot(2,2,4)
plot(log(t),A_noisy,'*')
title('Log-Linear Fit')
xlabel('ln(Time (years))')
ylabel('Mass (mg)')
axis([4 10 0 25])
fprintf('The function resulting in a plot, closest to linear is EXPONENTIAL.')
fprintf('\nOtherwise, shown as the plot with linear x and natural log of y.\n')
%coeffitiants represent m (slope) and b (y intercept) of linearized
%equation
p=polyfit(t,log(A_noisy),1);
fprintf('First order polynomial fit returns \n m= %0.06f \n b= %0.06f',p(1),p(2))
%to get the true m and b out of polyfit, we revurse the form it is given
%in. m is linear in the exponential function, so we leave m the way it is.
b=exp(p(2));
m=p(1);
fprintf('\nThe resulting Anot = %0.06f \n The resulting r= %0.06f',b,m)
%Original equation reconstructed
A=@(t) m.*exp(b.*t);
figure(2);
hold on
plot(t,A_noisy,'*r');
title('Mass vs Time, Fitted Polynomial')
xlabel('Time (years)')
ylabel('Mass (mg)')
figure(2);
hold on
fplot(A,[min(t), max(t)],'-b')
0 Commenti
Risposta accettata
Star Strider
il 16 Mag 2019
The problem appears to be in your definition of ‘b’ that in your current code is 20.3, causing ‘A’ to rapidly increase to the floating point upper limit.
I believe you have ‘m’ and ‘b’ reversed. If you define them as:
m=exp(p(2));
b=p(1);
you will get the plot you are likely expecting
2 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Surface and Mesh Plots in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!