Matlab giving incorrect graph for exponential below function

4 views (last 30 days)
Script 1
function [Ddv_Div] = neta_exp1(t,D)
a = 30;
Ddv_Div = t*exp(-t^2/(2*a^2));
end
Scirpt 2
clear all
tspan = [0 ,90]; %minutes
IC = 0;
[t, y] = ode23('neta_exp1',tspan, IC );
figure
plot(t, y(:,1),'k')
title('Exponential Curve');
legend('A');
xlabel('Time(min)');
ylabel('Exponential Equation');
Problem:- When I solve this equation on matlab, I get a plot but its not plotting correctly with its y-axis value at x-axis value at that time.
and When I plot on Excel sheet, the Slope of the curve is different than matlab.
Please help me to know the exact reason why it is not getting correct plot with this particular equation?
  2 Comments
dpb
dpb on 16 Mar 2019
What makes you think it isn't right? Looks perfectly reasonable to me (albeit I didn't compute another solution to compare to, I did change the constant a in the function to 10 and 100 from initial 30 and the solution appeared to change just as I'd expect it to...
Gaurav Paturkar
Gaurav Paturkar on 17 Mar 2019
Hello, Well its correct that if we change the constant value, plot changes as per constant value.
I said its incorrect becuase if we solve the integral on paper or on Excel sheet, I get the following values at time t and with this value, curve looks like decreasing with negative slope.
But on Matlab the curve plotting value goes on increasing with postive slope.
So I consider that, Curve plotting on Excel sheet is more correct than MATLAB. So I said matlab plotting wrong curve.
t y = -BG^2*e^(-t^2/(2*BG^2))
0 -900.00
5 -912.59
10 -951.41
15 -1019.83
20 -1123.96
25 -1273.62
30 -1483.85
35 -1777.48
40 -2189.18
45 -2772.20
50 -3609.35
55 -4831.68
60 -6650.15
65 -9410.84
70 -13692.68
75 -20483.91
80 -31506.54
85 -49825.57
90 -81015.42

Sign in to comment.

Answers (1)

John D'Errico
John D'Errico on 17 Mar 2019
Edited: John D'Errico on 17 Mar 2019
Sorry. But whatever you did in Excel is apparently flat out wrong. Lets just think about this, for a moment. Does it make even remote sense that the curve should be a decreasing function, thus a negative slope? You do say that is the result you got when you solved it on paper, and in Excel, did you not?
We see that t runs from 0-90, so ALWAYS non-negative t.
Then we see dy/dt, computed as:
a = 30;
Ddv_Div = t*exp(-t^2/(2*a^2));
t is positive, and exp(anything real) is a positive number. The product of two positive real numbers is, well also positive.
So the derivative of the function is ALWAYS positive for positive t.
Is it possible to have a function to have a negative slope, yet ALWAYS a positive first derivative over that interval? Yeah, right.
So I have no idea what you solved on paper, but you got something wrong when you did it yourself, and when you used Excel. At least, in comparison to what you solved in MATLAB. The mathematics cannot lie. Either the derivative you provided is simply wrong, or you made a big mistake when you solved it yourself. What I cannot do is check your solution, since we have not been given the equations you are trying to solve, nor have we been told what you did on paper. I do know that something you have done is incorrect, and MATLAB did not do the wrong thing with what you gave it.
  5 Comments
John D'Errico
John D'Errico on 18 Mar 2019
As both have said in my absence, your solution for y must be incorrect. You claim this to be true:
y = -a^(-t^2/(2*a^2))
which clearly iscompletely wrong, even if you have made a typo, and you meant to write:
y = -a^2*exp(-t^2/(2*a^2))
it would be flat out wrong.
I'm sorry, but that is impossible, because, as I showed in my answer, it is inconsistent with the derivative you show. The function has a positive derivative for positive t. So now let us solve the differential equation that you posed.
syms a t f(t)
dsolve(diff(f) == t*exp(-t^2/(2*a^2)),f(0) == 0,t)
ans =
a^2 - a^2*exp(-t^2/(2*a^2))
That is true, for any value of a. If we factor out a^2, we see
y = a^2*(1 - exp(-t^2/(2*a^2)))
This is as Torsten stated.
Your problem is that you solved the equation incorrectly! This is what I stated in my answer. In your solution, you dropped out the constant term. I'm sorry, but you cannot just arbitrarily drop out constant terms.
We can check it of course. If we differentiate that solution, we must get the derivative we started with.
diff(a^2*(1 - exp(-t^2/(2*a^2))),t)
ans =
t*exp(-t^2/(2*a^2))
Does the solution I have found above satisfy the initial condition?
y = a^2*(1 - exp(-t^2/(2*a^2)));
subs(y,t,0)
ans =
0
Of course it does. So what I wrote is indeed the solution to the differential equation you posed.
Now, suppose we arbitrarily dropped out that constant term, giving us what you seem to have have claimed is the solution? The derivative would be the same, but the initial condition would now fail!
So I'm sorry, but you made a mistake in your solution. What you wrote in Excel is simply wrong. What you did on paper is simply wrong. Putting an incorrect equation into a spreadsheet does not make it the correct solution to the problem. Have you never heard the phrase "Garbage in, garbage out"?

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!