Clear Filters
Clear Filters

Empty plot is showing because of my er

1 view (last 30 days)
Hello guys , is there a way i can store my er as a vector that i can later on use to plot with N ? thank you for your suggestions. The code is shown below. I just want to be able to use er in the for loop to plot with N , how can i retrieve the er values into a vector
N=[10,20,40,80];
for i = 1:length(N)
x = (0:N(i))./N(i);
xsamples = 0:0.005:1;
y = f(xsamples);
fxn = Lagpoly(xsamples,x,y,N);
er= max(abs(fxn-y));
end
plot(N, er, '*-', 'DisplayName', 'Data Points');
%plot(N,N);
%set(gca, 'XScale', 'log')
%set(gca, 'YScale', 'log')
function y = f(xsamples)
y = 1 ./ (1+ (xsamples.*xsamples));
end
  1 Comment
Dyuman Joshi
Dyuman Joshi on 30 Nov 2023
Edited: Dyuman Joshi on 30 Nov 2023
Even if the er is a scalar, the code should plot something, see below.
If you get an empty plot, your data could have NaN values.
Please attach the defition of the parameter Lagpoly, so that we can run the code and reproduce the error.
plot(1:10, 10, '*-')

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 30 Nov 2023
Index er. This works:
% Initialization steps.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 18;
N = [10, 20, 40, 80];
for k = 1:length(N)
x = (0:N(k))./N(k);
xsamples = 0:0.005:1;
y = f(xsamples);
% fxn = Lagpoly(xsamples,x,y,N);
fxn = rand;
er(k) = max(abs(fxn-y));
end
% Plot on a log-log scale. Or use plot() for a linear scale.
loglog(N, er, '*-', 'DisplayName', 'Data Points',...
'LineWidth', 2, 'MarkerSize', 30);
grid on;
xlabel('N', 'FontSize', fontSize);
ylabel('er', 'FontSize', fontSize);
title('er vs. N', 'FontSize', fontSize);
%=======================================================================
function y = f(xsamples)
y = 1 ./ (1+ (xsamples.*xsamples));
end
Since you didn't provide Lagpoly I had to just use a random number to test it. Replace the rand line with your Lagpoly line of code.
To learn other fundamental concepts, invest 2 hours of your time here:

More Answers (1)

Torsten
Torsten on 30 Nov 2023
Edited: Torsten on 30 Nov 2023
What x-y data are given to build the Lagrange Polynomial and in which x-data do you want to interpolate ? I assume that x-y are given arrays to build the Lagrange Interpolation upon and that you want to interpolate in xsamples. But your code takes xsamples and y to build the Lagrange polynomial and x for evaluation. I don't think this is what you want.
Further, you have to save "er" for each value of i.
Use
er(i)= max(abs(fxn-y));
instead of
er= max(abs(fxn-y));

Categories

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!