Calculating and Adding Percent Error to a Graph
72 views (last 30 days)
Show older comments
The Percent Error = 100*abs(exact solution-Approximate solution)/Exact Solution. I am writing a Euler method approximation and I need to know how best to present this. I keep getting the error message that "matrix dimensions must agree". Here is my code.
%Script that demonstrates Euler integration for a first order problem using
%The problem to be solved is:
%This problem has a known exact solution
function ystar = Eulermethod20(n)
t=0:h:5;%Initialize time variable
clear ystar;%wipe out old variable
ystar(1)=1.0;%Initial condition (same for approximation)
for i=1:length(t)-1, %Set up "for" loop
k1=2-exp(-4*t(i))-2*ystar(i); %Calculate the derivative
ystar(i+1)=ystar(i)+h*k1;%Estimate new value of y
%Plot approximate and exact solutions
The program runs, but the percent error doesn't calculate correctly. I need to calculate and show the error for this project and I was hoping someone can help. Thanks!
Fangjun Jiang on 23 Nov 2011
Since you have the y and ystar, you can do
Notice "./" is the element-wise division.
More Answers (1)
Naz on 23 Nov 2011
You are missing the 'dot' in the following expression:
Now this should work
Find more on Mathematics 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!